为 SessionWizardView 更正 <form action=" " URL

Correct <form action=" " URL for a SessionWizardView

让 Django SessionWizardView 将数据提交到我的数据库,我正在尝试隔离问题。

我注意到来自 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

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.

目前我正在使用 <form action="/surveyone/" method="post">,我认为这是正确的。

问题是我的视图名为 class SurveyWizardOne(SessionWizardView):,但如果我尝试在 form action 中使用它,我会在单击调查第一页上的“下一步”时立即收到错误消息。

问题:根据下面的action="/surveyone/"是否正确?

谢谢

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>

由于您的表单提交给同一个 url,您可以简单地使用 action=""。如果您愿意,可以使用 action="/surveyone/"

如果您不想在模板中对 url 进行硬编码,则需要 name your url patterns:

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

然后您可以在模板中使用 url 标签:

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