简体   繁体   中英

Correct <form action=“ ” URL for a SessionWizardView

I have been having some trouble getting a Django SessionWizardView to submit data to my database and I am trying to isolate the issue.

I note from the Form documentation

As well as its elements, a form must specify two things:

  • where: the URL to which the data corresponding to the user's input should be returned
  • how: the HTTP method the data should be returned by

and

Form data sent back to a Django Web site is processed by a view, generally the same view which published the form. This allows us to reuse some of the same logic.

Currently I am using <form action="/surveyone/" method="post"> which I believe is correct.

The issue is that my view is called class SurveyWizardOne(SessionWizardView): but if I try to use this in the form action I get an error as soon as I click Next on the first page of the survey.

Question: Based on the below is action="/surveyone/" correct?

Thanks

urls.py

url(r'^surveyone/$', SurveyWizardOne.as_view([
                                             SurveyFormIT1,
                                             SurveyFormIT2,
                                             Start,
                                             SurveyFormA, 
                                             SurveyFormB, 
                                             SurveyFormC, 
                                             SurveyFormD, 
                                             SurveyFormE,
                                             SurveyFormSpike1, 
                                             SurveyFormF1,
                                             SurveyFormF2,
                                             SurveyFormF3,
                                             SurveyFormDV1,
                                             SurveyFormF4,
                                             SurveyFormF5,
                                             SurveyFormF6,
                                             SurveyFormSpike2, 
                                             SurveyFormDV2,
                                             SurveyFormF7,
                                             SurveyFormF8,
                                             SurveyFormF9,
                                             SurveyFormDV3,
                                             SurveyFormDV4,
                                             SurveyFormDV5,
                                             SurveyFormG,
                                             SurveyFormH,
                                             SurveyFormI
                                             ])),  

views.py

class SurveyWizardOne(SessionWizardView):                             
    def get_context_data(self, form, **kwargs):
        context = super(SurveyWizardOne, self).get_context_data(form, **kwargs)                      
        step = int(self.steps.current)     



    ....
    ....


        return context 


    def done(self, form_list, **kwargs):
        return render(self.request, 'Return_to_AMT.html', {
            'form_data': [form.cleaned_data for form in form_list],            
        })

wizard_form.html

{% extends "base.html" %}
{% load i18n %}
{% block head %}
{{ wizard.form.media }}
{% endblock %}
{% block content %}


<div class="main_content">   

<p>Page: {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>

<form action="/surveyone/" method="post">{% csrf_token %}
            <table>   
                {{ wizard.management_form }}
                    {% if wizard.form.forms %}
                        {{ wizard.form.management_form }}
                        {% for form in wizard.form.forms %}
                            {{ form }}
                        {% endfor %}
                    {% else %}
                        {{ wizard.form }}
                {% endif %}   
            </table>

Since your form is submitting to the same url, you can simply use action="" . If you prefer, you can use action="/surveyone/"

If you don't want to hardcode the url in your template, then you need to name your url patterns :

url(r'^surveyone/$', SurveyWizardOne.as_view([
                                             SurveyFormIT1,
                                             SurveyFormH,
                                             ...
                                             ]), name="survey_one"),  

You can then use the url tag in your template:

action="{% url 'survey_one' %}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM