Commits

Benoît Bryon committed 3941235

Added fieldsets and field ordering examples in demo project

Comments (0)

Files changed (8)

demo/demoapp/templates/demoapp/base.html

           <h4><a href="{% url demoapp:as_table %}">{% trans "as_table" %}</a></h4>
           <h4><a href="{% url demoapp:as_ul %}">{% trans "as_ul" %}</a></h4>
           <h4><a href="{% url demoapp:as_p %}">{% trans "as_p" %}</a></h4>
+        <h3>{% trans "Fields order and form decomposition" %}</h3>
+          <h4><a href="{% url demoapp:fields_order %}">{% trans "Fields order" %}</a></h4>
+          <h4><a href="{% url demoapp:fieldsets %}">{% trans "Fieldsets" %}</a></h4>
     </div>
   </body>
 </html>

demo/demoapp/templates/demoapp/comparison.html

 
 {% block content %}
 <h1>{{ title }}</h1>
-<p>{{ description }}</p>
+<p>{{ description|safe }}</p>
 
 <div class="builtin" style="width:50%;margin:0;padding:0;float:left;">
   <h2>{% trans "Django's builtins" %}</h2>

demo/demoapp/templates/demoapp/includes/fields_order_builtin.html

+<form>
+  {{ form.non_field_errors }}
+
+  {% with form.name as field  %}
+    <div class="fieldWrapper">
+      {{ field.errors }}
+      <label for="id_subject">{{ field.label }}</label>
+      {{ field }}
+      <span class="helptext">{{ field.help_text }}</span>
+    </div>
+  {% endwith %}
+
+  {% with form.domain as field  %}
+    <div class="fieldWrapper">
+      {{ field.errors }}
+      <label for="id_subject">{{ field.label }}</label>
+      {{ field }}
+      <span class="helptext">{{ field.help_text }}</span>
+    </div>
+  {% endwith %}
+
+  <input type="submit">
+</form>

demo/demoapp/templates/demoapp/includes/fields_order_formrenderingtools.html

+{% load form_layouts %}
+
+<form>
+  {% form_errors %}
+  {% field field=form.name %}
+  {% field field=form.domain %}
+  <input type="submit">
+</form>

demo/demoapp/templates/demoapp/includes/fieldsets_builtin.html

+<form>
+  {{ form.non_field_errors }}
+
+  <fieldset>
+    <legend>Message content</legend>
+    
+    {% with form.subject as field  %}
+      <div class="fieldWrapper">
+        {{ field.errors }}
+        <label for="id_subject">{{ field.label }}</label>
+        {{ field }}
+        <span class="helptext">{{ field.help_text }}</span>
+      </div>
+    {% endwith %}
+
+    {% with form.message as field  %}
+      <div class="fieldWrapper">
+        {{ field.errors }}
+        <label for="id_subject">{{ field.label }}</label>
+        {{ field }}
+        <span class="helptext">{{ field.help_text }}</span>
+      </div>
+    {% endwith %}
+  </fieldset>
+
+  <fieldset>
+    <legend>Sender options</legend>
+    
+    {% with form.sender_email as field  %}
+      <div class="fieldWrapper">
+        {{ field.errors }}
+        <label for="id_subject">{{ field.label }}</label>
+        {{ field }}
+        <span class="helptext">{{ field.help_text }}</span>
+      </div>
+    {% endwith %}
+
+    {% with form.sender_nickname as field  %}
+      <div class="fieldWrapper">
+        {{ field.errors }}
+        <label for="id_subject">{{ field.label }}</label>
+        {{ field }}
+        <span class="helptext">{{ field.help_text }}</span>
+      </div>
+    {% endwith %}
+
+    {% with form.cc_myself as field  %}
+      <div class="fieldWrapper">
+        {{ field.errors }}
+        <label for="id_subject">{{ field.label }}</label>
+        {{ field }}
+        <span class="helptext">{{ field.help_text }}</span>
+      </div>
+    {% endwith %}
+  </fieldset>
+  
+  <input type="submit">
+</form>

demo/demoapp/templates/demoapp/includes/fieldsets_formrenderingtools.html

+{% load form_layouts %}
+
+<form>
+  {% form_errors %}
+  
+  <fieldset>
+    <legend>Message content</legend>
+    {% field_list fields="subject,message" %}
+  </fieldset>
+
+  <fieldset>
+    <legend>Sender options</legend>
+    {% field_list fields="sender_email,sender_nickname,cc_myself" %}
+  </fieldset>
+  
+  <input type="submit">
+</form>

demo/demoapp/urls.py

     url(r'^as-table/$', 'as_table', name='as_table'),
     url(r'^as-ul/$', 'as_ul', name='as_ul'),
     url(r'^as-p/$', 'as_p', name='as_p'),
-    
+    url(r'^fields-order/$', 'fields_order', name='fields_order'),
+    url(r'^fieldsets/$', 'fieldsets', name='fieldsets'),
 )

demo/demoapp/views.py

     form = FakeContactForm({'subject': u''})
     template = 'as_p'
     return comparison_page(request, title, description, form, template)
+
+
+def fields_order(request):
+    title = _("Reordering fields")
+    description = _("""Often, you don't want to reoder fields in Python code because:</p>
+<ul>
+  <li>it resides in Django,</li>
+  <li>or in a third party module (don't want to fork it!),</li>
+  <li>or it is a ModelForm (don't want to alter the model).</li>
+</ul>
+<p>Reordering form fields in a Django template is boring: you have to reproduce all the template code for each field! And don't forget error messages, help texts...</p>
+<p>This example uses a ModelForm on django.contrib.sites.models.Site. We *just* want to display 'display name' before 'domain name'.</p>
+<p>Maybe you are thinking that you had better include a template file instead of repeating code... That is exactly the purpose of Django-formrenderingtools ;)""")
+    from django.contrib.sites.models import Site
+    from django.forms import ModelForm
+    class SiteForm(ModelForm):
+        class Meta:
+            model = Site
+    form = SiteForm({'name': u'example'})
+    template = 'fields_order'
+    return comparison_page(request, title, description, form, template)
+
+
+def fieldsets(request):
+    title = _("Displaying fieldsets")
+    description = _("""Form decomposition with Django builtins: you have to rewrite all fields manually... Need a {% field_list %} template tag?</p>
+<p>In this example, we want to create two fieldsets: one to group message and subject, another one to group fields about sender.""")
+    form = FakeContactForm({'subject': u''})
+    template = 'fieldsets'
+    return comparison_page(request, title, description, form, template)