0

Friends, I searched a lot and couldn't find it, so I decided to ask here.

I need to create a form on a page made in Django (Python), that it will create a file based on a "skeleton" Python code and change only the specific fields that I put in bold and save the same.

Follows the code "skeleton".

@staticmethod
def required_slots (tracker: Tracker) -> List [Text]:
    ** Field1 ** = []
@staticmethod
def required_slots (tracker: Tracker) -> List [Text]:
    ** Field2 ** = []
@staticmethod
def required_slots (tracker: Tracker) -> List [Text]:
    ** Field3 ** = []

I will create a page with a form with the fields: Field1, Field2 and Field3 and they will be filled with the following information:

Field1 = Name Field2 = Age Field3 = Sex

The generated skeleton code should look like this:

@staticmethod
def required_slots (tracker: Tracker) -> List [Text]:
    ** Name ** = []
@staticmethod
def required_slots (tracker: Tracker) -> List [Text]:
    ** Age ** = []
@staticmethod
def required_slots (tracker: Tracker) -> List [Text]:
    ** Sex ** = []

Could someone give me a way of how to make?

2
  • Welcome to SO. As is, your question is either way too broad (if you're asking for full solution) or unclear. Please edit your post to specify what you've tried and where you are hitting a roadblock exactly (cf stackoverflow.com/help/how-to-ask) Commented Aug 28, 2019 at 13:19
  • 1
    Just a hint nonetheless: django comes with a template engine that works for just any kind of text format - it's not restricted to html... Commented Aug 28, 2019 at 13:21

1 Answer 1

1

If I understand your question, a function like the following (called by your form handler after validation) could give you an idea as to how to do that:

from django.template import Template, Context


def make_skeleton(form_data):
    context = Context({'Name': form_data['Field1'], 'Age': form_data['Field2'], 'Sex': form_data['Field3']})
    template = Template('''
@staticmethod
def required_slots (tracker: Tracker) -> List [Text]:
    {{ Name }} = []
@staticmethod
def required_slots (tracker: Tracker) -> List [Text]:
    {{ Age }} = []
@staticmethod
def required_slots (tracker: Tracker) -> List [Text]:
    {{ Sex }} = []
''')
    with open('/path/to/skeleton.py', 'wt') as fobj:
        fobj.write(template.render(context))
Sign up to request clarification or add additional context in comments.

1 Comment

Small and perfectly formed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.