Search code examples
pythondjangodjango-formsdjango-templatesdjango-formwizard

How to use multiple forms with a Form Wizard form in the same template (Django)


I'm using multiple forms in the same template and they all work until I add my Form Wizard, when it becomes that either the FormWizard - form works or the rest of the forms works but not both simultaniousely.

When I have the URL's with postid (?P\d+) -url placed prior to the ContactWizard.as_view -urls the forms in the view - function one_labeling are displayed but not the Form Wizard/ContactWizard.as_view in views.py 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'),

and vice versa, when the URL's for the Form Wizard is placed before the postID - url for the forms in the view function one_labeling then the FormWizard is displayed (and works) but the other forms aren't displayed/evoked.

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

I'm not sure on how to prefix the Wizard Form so that I could use it as {{ form9.as_p }} like with {{ form3.as_p }} instead of {{ form1 }}{{ wizard.management_form }} or {{ form }} in the done.html below, so that it would work simultaniousely with the other forms in template one_labeling_index.html.

in template done.html

{% extends 'base_one_labeling.html' %}

{% block content %}

   {% for form in form_data %}

       {{ form }}

   {% endfor %}

{% endblock %}

in 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'], '[email protected]',
              fail_silently=False)

    return form_data

in views.py,LabelingIndex,function evoking template 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)

in views.py,one_labeling, views function 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))

in template 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 %}

in template 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>

Solution

  • The line

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

    matches any url beginning by "label", so any of your other url you put after are never matched.
    Also, except the trailing "/", the lines:

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

    match the same thing.
    So you have to order carefully your urls, and distinguish them somehow to avoid any ambiguity.