简体   繁体   English

正确

[英]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. 我在让Django SessionWizardView将数据提交到我的数据库时遇到一些麻烦 ,并且我试图找出问题所在。

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 其中:应将与用户输入相对应的数据返回到的URL
  • how: the HTTP method the data should be returned by 方式:数据应通过的HTTP方法

and

Form data sent back to a Django Web site is processed by a view, generally the same view which published the form. 发送回Django网站的表单数据由视图处理,该视图通常与发布表单的视图相同。 This allows us to reuse some of the same logic. 这使我们可以重用某些相同的逻辑。

Currently I am using <form action="/surveyone/" method="post"> which I believe is correct. 目前,我使用的是<form action="/surveyone/" method="post">我认为是正确的。

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. 问题是我的视图称为class SurveyWizardOne(SessionWizardView):但是,如果我尝试在form action使用此视图,则只要在调查的第一页上单击“下一步”,就会收到错误消息。

Question: Based on the below is action="/surveyone/" correct? 问题:基于以下内容, action="/surveyone/"是否正确?

Thanks 谢谢

urls.py 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 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 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="" . 由于您的表单提交到相同的URL,因此您只需使用action="" If you prefer, you can use action="/surveyone/" 如果愿意,可以使用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: 然后,您可以在模板中使用url标记:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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