Commits

Benoît Bryon committed 5919ff5

Added 'displaying form errors' example in demo

Comments (0)

Files changed (9)

demo/demoapp/templates/demoapp/base.html

     <div id="menu" style="clear:both;">
       <h2>{% trans "Table of contents" %}</h2>
         <h3><a href="{% url demoapp:simple %}">{% trans "Simplest use case" %}</a></h3>
+        <h3><a href="{% url demoapp:errors %}">{% trans "Displaying errors" %}</a></h3>
     </div>
   </body>
 </html>

demo/demoapp/templates/demoapp/includes/errors_builtin.html

+<form>
+  <table>
+    {{ form }}
+  </table>
+  <input type="submit">
+</form>

demo/demoapp/templates/demoapp/includes/errors_formrenderingtools.html

+{% load form_layouts %}
+
+<form>
+  {% form %}
+  <input type="submit">
+</form>

demo/demoapp/templates/demoapp/includes/simple_builtin.html

+<form>
+  <table>
+    {{ form }}
+  </table>
+  <input type="submit">
+</form>

demo/demoapp/templates/demoapp/includes/simple_formrenderingtools.html

+{% load form_layouts %}
+
+<form>
+  {% form %}
+  <input type="submit">
+</form>

demo/demoapp/templates/demoapp/simple_builtin.html

-<form>
-  <table>
-    {{ form }}
-  </table>
-  <input type="submit">
-</form>

demo/demoapp/templates/demoapp/simple_formrenderingtools.html

-{% load form_layouts %}
-
-<form>
-  {% form %}
-  <input type="submit">
-</form>

demo/demoapp/urls.py

 
 urlpatterns += patterns('demo.demoapp.views',
     url(r'^simple/$', 'simple', name='simple'),
-    url(r'^simple/builtin/$', 'simple_builtin', name='simple_builtin'),
-    url(r'^simple/formrenderingtools/$', 'simple_formrenderingtools', name='simple_formrenderingtools'),
+    url(r'^errors/$', 'errors', name='errors'),
 )

demo/demoapp/views.py

 
 
 def get_template_string(template_name):
-    """Returns the source code of the given template name. Template filenames
-    are relative to the demoapp templates folder."""
+    """Utility method that returns the source code of the given template name.
+
+    Template filenames are relative to the demoapp templates folder."""
     base_path = os.path.dirname(__file__)
     filename = os.path.join(base_path, 'templates', template_name)
     with open(filename) as f:
 
 
 def demo_page(request, title, description, form, template):
-    """Generic view to display a demo page."""
+    """Old-style generic view to display a demo page."""
     context_data = {
         'title': title,
         'description': description,
 
 
 def comparison_page(request, title, description, form, template):
-    """Generic view to display a comparison page."""
+    """Old-style generic view to display a comparison page."""
     context_data = {
         'title': title,
         'description': description,
         'form': form,
-        'builtin_template': 'demoapp/%s_builtin.html' % template,
-        'formrenderingtools_template': 'demoapp/%s_formrenderingtools.html' % template,
+        'builtin_template': 'demoapp/includes/%s_builtin.html' % template,
+        'formrenderingtools_template': 'demoapp/includes/%s_formrenderingtools.html' % template,
     }
     context_data['builtin_source'] = get_template_string(context_data['builtin_template'])
     context_data['formrenderingtools_source'] = get_template_string(context_data['formrenderingtools_template'])
     return comparison_page(request, title, description, form, template)
 
 
-def simple_builtin(request):
-    title = _("Simple form with Django's builtin {{ form }}")
-    description = _("The following example illustrates the minimal way to display a form with Django's builtins.")
-    form = FakeContactForm()
-    template = 'demoapp/simple_builtin.html'
-    return demo_page(request, title, description, form, template)
-
-def simple_formrenderingtools(request):
-    title = _("Simple form with Django-formrenderingtools")
-    description = _("The following example illustrates the minimal way to display a form with Django-formrenderingtools. Load the library. Replace {{ form }} with {% form %}.")
-    form = FakeContactForm()
-    template = 'demoapp/simple_formrenderingtools.html'
-    return demo_page(request, title, description, form, template)
+def errors(request):
+    title = _("Displaying form errors")
+    description = _("Let's see what happens with an invalid form. Notice that nothing changed in both template code.")
+    form = FakeContactForm({'subject': u''})
+    template = 'errors'
+    return comparison_page(request, title, description, form, template)