Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 17th, 2012  |  syntax: None  |  size: 3.45 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Django form wizard - customized form layout for each step
  2. {% block content %}
  3. # block step: variable text for each step
  4. {% block step %}{% endblock %}
  5. <p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
  6. <form action="" method="post">{% csrf_token %}
  7. <table>
  8. {{ wizard.management_form }}
  9. {% if wizard.form.forms %}
  10.     {{ wizard.form.management_form }}
  11.     {% for form in wizard.form.forms %}
  12.         # block form_if: shows a complete customized form layout for each step
  13.         {% block form_if %}{% endblock %}
  14.     {% endfor %}
  15. {% else %}
  16.     # block form_else: shows a complete customized form layout for each step
  17.     {% block form_else %}{% endblock %}
  18. {% endif %}
  19. </table>
  20. {% if wizard.steps.prev %}
  21. <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans    "first step" %}</button>
  22. <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
  23. {% endif %}
  24. <input type="submit" value="{% trans "submit" %}"/>
  25. </form>
  26. {% endblock %}
  27.        
  28. {{ wizard.management_form }}
  29. {% if wizard.form.forms %}
  30.     {{ wizard.form.management_form }}
  31.     {% for form in wizard.form.forms %}
  32.         # block form_if: shows a complete customized form layout for each step
  33.         {% with myform=form %}
  34.         {% block form_if %}{% endblock %}
  35.         {%e endwith %}
  36.     {% endfor %}
  37. {% else %}
  38.     # block form_else: shows a complete customized form layout for each step
  39.     {% with myform=form %}    
  40.     {% block form_else %}{% endblock %}
  41.     {% endwith %}
  42. {% endif %}
  43.        
  44. class TestFormWizard(SessionWizardView):
  45.  
  46.     def get_template_names(self):
  47.         if self.steps.current == 1:
  48.             return 'step1_form.html'
  49.         if self.steps.current == 2:
  50.             return 'step2_form.html'
  51.         return 'wz_form.html'
  52.        
  53. <form enctype="multipart/form-data" action="" method="post">{% csrf_token %}
  54.     <table>
  55.         {{ wizard.management_form }}
  56.         {% if wizard.form.forms %}
  57.             {{ wizard.form.management_form }}
  58.             {% for form in wizard.form.forms %}
  59.                 <!-- block below accesses a customized form layout for each step -->
  60.                 {% block form_if %}{% endblock %}
  61.             {% endfor %}
  62.         {% else %}
  63.             <!-- block below accesses a customized form layout for each step -->
  64.             <!-- using the with statement makes it possible to use the same layout used in the form_if block -->
  65.             {% with form=wizard.form %}
  66.             {% block form_else %}{% endblock %}
  67.             {% endwith %}
  68.         {% endif %}
  69.     </table>
  70.     {% if wizard.steps.prev %}
  71.     <button name="wizard_goto_step" value="{{ wizard.steps.first }}" class="button">{% trans "first step" %}</button>
  72.     <button name="wizard_goto_step" value="{{ wizard.steps.prev }}" class="button">{% trans "prev step" %}</button>
  73.     {% endif %}
  74.     <div>
  75.         <input type="submit" value="{% trans "submit" %}" class="button"/>
  76.     </div>  
  77. </form>
  78.        
  79. {% extends "base_wizard.html" %}
  80.  
  81. {% block form_if %}{% block form_else %}
  82.     <fieldset>
  83.         <legend>Title</legend>
  84.         <div>
  85.             <label>{{ form.field.label }}:<p class="note">{{ form.field.help_text }}</p></label> {{ form.field }}
  86.             <p class="error">
  87.                 {% if form.field.errors %}
  88.                     {% for error in form.field.errors %}
  89.                         {{ error }}
  90.                     {% endfor %}
  91.                 {% endif %}
  92.             </p>
  93.         </div>
  94.     </fieldset>
  95. {% endblock %}{% endblock %}