更正<form action =“”url =“”for =“”a =“”sessionwizardview =“”

时间:2015-05-20 16:06:44

标签: django django-forms django-templates django-views

=“”

I have been having some trouble获取Django SessionWizardView将数据提交到我的数据库,我试图找出问题。

我从Form documentation

注意到
  

除了元素外,表单还必须指定两个内容:

     
      
  • 其中:应返回与用户输入相对应的数据的URL
  •   
  • 如何:
  • 返回数据的HTTP方法   

  

发送回Django网站的表单数据由视图处理,   通常与发布表格的观点相同。这允许我们   重用一些相同的逻辑。

目前我正在使用<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>

1 个答案:

答案 0 :(得分:1)

由于您的表单提交到同一网址,因此您只需使用action=""即可。如果您愿意,可以使用action="/surveyone/"

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

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

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

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