如何在同一模板中使用带有表单向导表单的多个表单(Django)

时间:2015-10-19 11:31:48

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

我在同一个模板中使用了多个表单,它们一直有效,直到我添加我的表单向导,当它变为FormWizard - 表单工作或其余表单工作但不是两个同时。

如果我的URL与postid(?P \ d +)-url放在ContactWizard.as_view之前 - 在视图中显示表单 - 函数one_labeling显示但不显示views.py中的表单向导/ ContactWizard.as_view class ContactWizard(SessionWizardView)

url(r'^label$', LabelingIndex),
url(r'^label(?P<postID>\d+)$',one_labeling),# <----- here
url(r'^label',ContactWizard.as_view([Form1, Form2, Form3])),
url(r'^label(?P<one_labeling>\d+)/$', 'formwizard.views.one_labeling'),

反之亦然,当表单向导的URL放在postID - 视图函数one_labeling中的表单的url之前,然后显示FormWizard(并且工作),但不显示/激发其他表单。

url(r'^label$', LabelingIndex),
url(r'^label',ContactWizard.as_view([Form1,Form2, Form3])),          
url(r'^label(?P<one_labeling>\d+)/$', 'formwizard.views.one_labeling'),
url(r'^label(?P<postID>\d+)$',one_labeling), #<----- here

我不确定如何为向导表单添加前缀,以便我可以将其用作{{form9.as_p}},例如{{form3.as_p}},而不是{{form1}} {{wizard.management_form在下面的done.html中,或者{{form}},以便它可以与模板one_labeling_index.html中的其他表单同步工作。

模板done.html中的

{% extends 'base_one_labeling.html' %}

{% block content %}

   {% for form in form_data %}

       {{ form }}

   {% endfor %}

{% endblock %}
在views.py中

class ContactWizard(SessionWizardView):

    template_name = "one_labeling_index.html"

    def done(self, form_list, **kwargs):


        form_data = process_form_data(form_list)

        return render_to_response("done.html",{"form_data":form_data})
def process_form_data(form_list):
    form_data = [form.cleaned_data for form in form_list]

    logr.debug(form_data[0]['subject'])
    logr.debug(form_data[1]['sender'])
    logr.debug(form_data[2]['message'])

    send_mail(form_data[0]['subject'],form_data[1]['sender'],
              form_data[2]['message'], 'xxxx@gmail.com',
              fail_silently=False)

    return form_data

在views.py,LabelingIndex中,函数唤起模板labeling_index.html

def LabelingIndex(request):
    #labelings, objects in class Labeling() in models.py
    labelings = Labeling.objects.all()

    c ={"labelings":labelings}
    c.update(csrf(request))
    return render(request,"labeling_index.html", c)

在views.py,one_labeling中,查看函数one_labeling

def one_labeling(request,postID):
       #one_labeling, object in class Labeling  
      one_labeling= Labeling.objects.get(id=postID)
      template = one_labeling_index.html


      if request.method == "POST":
          # forms used in one_labeling_index.html

          form = SentenceForm(request.POST, prefix="sentence")
          form2 = OpenFileForm(request.POST, prefix="file")

          form3 = LabelingForm(request.POST,prefix="form3")
          form9 =LabelingForm2(request.POST,prefix="labeling") 
          if form.is_valid() and 'button1' in request.POST:
                   # do ....
          if form3.is_valid() and 'button2' in request.POST:
                   post_one_labeling(request.POST, one_labeling)

          else:
              form = SentenceForm()
              form2 = OpenFileForm()
              form3 = LabelingForm()

              form9 = LabelingRRGForm2()
          c = {"one_labeling":one_labeling,"form3":form3,"form":form,"form9":form9...... }

          c.update(csrf(request))


         return render(request,template,c,context_instance=RequestContext(request))

在模板Labeling_index.html

{% for one_labeling in labelings %}


  # Link to new id (postid) - page; template one_labeling.html 
  &nbsp;&nbsp Subject: <a href="/label{{ one_labeling.id }}">{{ one_labeling.title }}<a/><br/>



{% endfor %}

在模板one_labeling_index.html

# extending base_one_labeling.html
{% extends "base_one_labeling.html" %}
{% block content %}

# form3
<form3 action="" method="post" enctype="multipart/form-data">{% csrf_token %}

    {{ form3.as_p }}

</form3>

# Form Wizard 
  <p>Label {{ wizard.steps.step1 }} out of {{ wizard.steps.count }} ({{ wizard.steps.step1 }}/{{ wizard.steps.count }})</p>
  {% for field in form %}
    {{field.error}}
  {% endfor %}

  <form action="" method="post" enctype="multipart/form-data">{% csrf_token %}



  <table>
  {{ wizard.management_form }}
  {% if wizard.form.forms %}
      {{ wizard.form.management_form }}
      {% for form1 in wizard.form.forms %}
          {{ form1 }}
      {% endfor %}
  {% else %}
      {{ wizard.form }}
  {% endif %}
  </table>
  {% if wizard.steps.prev %}


  {% endif %}




<html>
<body>

1 个答案:

答案 0 :(得分:0)

该行

url(r'^label',ContactWizard.as_view([Form1, Form2, Form3])),  

匹配以“label”开头的任何网址,因此您放置的任何其他网址都不会匹配 另外,除了尾部的“/”之外,还有以下几行:

url(r'^label(?P<one_labeling>\d+)/$', 'formwizard.views.one_labeling'),
url(r'^label(?P<postID>\d+)$',one_labeling), #<----- here

匹配同样的事情。
因此,您必须仔细订购您的网址,并以某种方式区分它们以避免任何歧义。