【问题标题】:How to use multiple forms with a Form Wizard form in the same template (Django)如何在同一个模板中使用多个表单和表单向导表单(Django)
【发布时间】:2016-01-17 17:36:37
【问题描述】:

我在同一个模板中使用多个表单,它们都可以工作,直到我添加我的表单向导,当它变成 FormWizard - 表单工作或其余表单工作但不能同时工作时。

当我将带有 postid (?P\d+) 的 URL 放置在 ContactWizard.as_view -urls 之前的 URL 时,视图中的表单会显示 - 函数 one_labeling 会显示,但不会显示 views.py 中的表单向导/ContactWizard.as_view类 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 }} 或 {{ form }} 在下面的 done.html 中,以便它可以与模板 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>

【问题讨论】:

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


    【解决方案1】:

    线

    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
    

    匹配相同的东西。
    所以你必须仔细订购你的网址,并以某种方式区分它们以避免任何歧义。

    【讨论】:

    • 我尝试过以下方法;1) 重命名 url(r'^label_wiz(?P\d+)/$', 'formwizard.views.one_labeling'),导致向导表单仅与 url /label_wiz 一起使用,而不与其他带有 url /label 的表单一起使用 2) 命名 url ^label(?P\d+)/$', 'formwizard.views.one_labeling', name= "label1"),url(r'^label(?P\d+)$',one_labeling, name='label2'),但我不确定如何在模板 one_labeling_index.html 中指定它。
    • 3) 向 /label 添加斜线,因此 /label/ 和 label/+1 不能解决问题。 4) 在向导表单中指定动作 action="/label" 时被命名为 "label2", name="label" 在 2).
    • @user1749431: 抱歉,这个表格让我很困惑,所以我不能给你进一步的帮助。
    • 我设法通过将 LabelingForms 字段添加到指定的 form_list 表单中来使用它。由于您所说的原因,无法找到同时运行两者的解决方案。
    猜你喜欢
    • 2013-09-17
    • 1970-01-01
    • 2013-01-16
    • 2012-05-16
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2015-11-27
    • 2019-01-08
    相关资源
    最近更新 更多