[Answered ]-How to run a python script like class in django?

1👍

For your create_template view, you can handle both HTTP GET and HTTP POST methods. You can also use Django’s built-in form handling to make this so much easier.

New create_template.html

...
          <form action="create_template" method="GET">
            {% csrf_token %}
            <h1>Create your template</h1>
            <div class="item">
              <table>
                {{ form.as_table }}
              </table>
            </div>
            <div class="btn-block">
              <input type="button" type="submit" value="Create and Download!"/>
            </div>
          </form>
...

New content of views.py

        from   django.forms import Forn, CharField, ChoiceField, IntegerField, 
        ...
    
            class TemplateForm(Form):
               name = CharField(label='Doc name')
               choices=[('1', 'Russia'), ('2', 'Germany'), ('3', 'France'),
                        ('4', 'Armenia'), ('5', 'USA')]
               country = ChoiceField(label='name_sheet', initial='5', choices=choices)
               choices = []
               for year in range (2000, 2050):
                   choices.append( (year, year) )
               year1 = ChoiceField(label='Starting Year', initial=2021, choices=choices)
               year2 = ChoiceField(label='Ending Year', initial=2022, choices=choices)
               columns = CharField(label='Column names')
            
            def create_template(request):
                if request.method == 'GET':
                    form = TemplateForm()
                    return render(request, 'my_app/create_template.html' form=form)
                else:
                    form = TemplateForm(request.POST)
                    <use form inputs as needed>
    ...

Leave a comment