将POST数据传递给表单向导时出错

时间:2013-08-30 17:46:02

标签: django-forms django-templates django-formwizard

我的初始表格如下

forms.py

class LoginForm(forms.Form):
  activity_no = forms.CharField()
  username = forms.CharField()

views.py

def test(request):
if request.method == 'POST' :
    form = LoginForm(request.POST)
    if form.is_valid() :
        act_no = form.cleaned_data['activity_no']
        username = form.cleaned_data['username']
        form_data = {}
        form_data['activity_no'] = act_no
        form_data['username'] = username

        return render_to_response("test1.html", { 'form_data' : form_data}, context_instance=RequestContext(request))
else:
     form = LoginForm()

return render_to_response("test.html", {'form': form }, context_instance=RequestContext(request))

的test.html

{% extends "admin/base.html" %}

{% block content %}

<form action="/todo/test/" method="post">{% csrf_token %}
    {{ form.as_p }}
<input type="submit" value="Submit" />
</form>

{% endblock %}

test1.html

 {% extends "admin/base.html" %}

 {% block content %}

 <form action="/todo/login/" method="post">{% csrf_token %}
     {% for name, data in form_data.items %}
       <input type="hidden" name={{name}} value={{data}}>
     {% endfor %}

     <input type="submit" value="Yes"/>
 </form>

 {% endblock %}

我填写此表单,显示文件的一些内容并将上述表单数据传递给表单向导 但是当我通过模板中的post方法将数据传递给表单向导时,我得到一个“ManagementForm Data missing”错误但是如果数据是通过GET方法传递的,那么我没有得到任何错误,但是如定义的那样,数据在GET方法的url中看到(在我的情况下,它包含我不想透露的用户名)

我的表单向导

class LoginWizard(SessionWizardView):

def __name__(self):
    """When using decorators, Django tries to get the name of the
       function and since we're a class, we'll fail. So add this method to
       compensate."""
    return 'LoginWizard'

template_name = "wizard_form.html"
def done(self, form_list, **kwargs) :
    form_data = process_form_data(form_list)

我的查询是如何处理表单向导中的帖子数据。 如果我遗漏了任何内容或者我需要任何其他信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

您需要在模板中添加{{ wizard.management_form }},如参考django wizard templates中所述。

像:

{% extends "admin/base.html" %}

{% block content %}

<form action="/todo/test/" method="post">{% csrf_token %}
    {{ wizard.management_form }}
    {{ form.as_p }}
<input type="submit" value="Submit" />
</form>

{% endblock %}

在向导的所有模板中添加。