1. Benoît Bryon
  2. django-formrenderingtools

Commits

Benoît Bryon  committed a5f9229

Started demo project with simple example

  • Participants
  • Parent commits d37ec9f
  • Branches default

Comments (0)

Files changed (10)

File demo/demoapp/forms.py

View file
+from django import forms
+from django.utils.translation import ugettext_lazy as _
+
+
+class FakeContactForm(forms.Form):
+    """A sample contact form which never validates."""
+    subject = forms.CharField(
+        label=_('Subject'),
+        max_length=100,
+    )
+    message = forms.CharField(
+        label=_('Message'),
+        widget=forms.Textarea()
+    )
+    sender_email = forms.EmailField(label=_('Sender email'))
+    sender_nickname = forms.CharField(
+        label=_('Sender nickname'),
+        required=False,
+    )
+    cc_myself = forms.BooleanField(
+        label=_('Copy to the sender'),
+        required=False,
+        help_text='Send a copy of the message to the sender.',
+    )
+    
+    def clean(self):
+        """This sample form never validates!"""
+        raise forms.ValidationError('Sorry, this sample form never validates!')

File demo/demoapp/templates/demoapp/base.html

View file
+{% load i18n %}
+<html>
+  <head>
+    <title>{% trans "Django-formrenderingtools demo" %}</title>
+  </head>
+  <body>
+    {% block content %}{% endblock content %}
+    <div id="menu">
+      <h2>{% trans "Table of contents" %}</h2>
+        <h3><a href="{% url demoapp:simple_builtin %}">{% trans "Simplest use case with Django builtins" %}</a></h3>
+        <h3><a href="{% url demoapp:simple_formrenderingtools %}">{% trans "Simplest use case with Django-formrenderingtools" %}</a></h3>
+    </div>
+  </body>
+</html>

File demo/demoapp/templates/demoapp/demo.html

View file
+{% extends "demoapp/base.html" %}
+{% load i18n %}
+
+{% block content %}
+<h1>{{ title }}</h1>
+<p>{{ description }}</p>
+
+<div class="source">
+  <h2>Source</h2>
+  <pre>{{ source }}</pre>
+</div>
+
+<div class="output">
+  <h2>Output</h2>
+  {% include template %}
+</div>
+{% endblock content %}

File demo/demoapp/templates/demoapp/index.html

View file
+{% extends "demoapp/base.html" %}
+{% load i18n %}
+
+{% block content %}
+<h1>{% trans "Django-formrenderingtools demo" %}</h1>
+<p>{% trans "This demonstration compares Django builtins and Django-formrenderingtools. Follow the guide..." %}</p>
+{% endblock content %}

File demo/demoapp/templates/demoapp/simple_builtin.html

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

File demo/demoapp/templates/demoapp/simple_formrenderingtools.html

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

File demo/demoapp/urls.py

View file
+from django.conf.urls.defaults import patterns, include, url
+
+urlpatterns = patterns('',
+    url(r'^$', 'django.views.generic.simple.direct_to_template',
+        {'template': 'demoapp/index.html'}, name='simple_builtin'),
+)
+
+urlpatterns += patterns('demoapp.views',
+    url(r'^simple/builtin/$', 'simple_builtin', name='simple_builtin'),
+    url(r'^simple/formrenderingtools/$', 'simple_formrenderingtools', name='simple_formrenderingtools'),
+)

File demo/demoapp/views.py

View file
-# Create your views here.
+import os.path
+
+from django.views.generic.simple import direct_to_template
+from django.template.loader import find_template
+from django.utils.translation import ugettext_lazy as _
+
+from demoapp.forms import FakeContactForm
+
+
+def get_template_string(template_name):
+    """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:
+        return f.read()
+
+
+def demo_page(request, title, description, form, template):
+    """Generic view to display a demo page."""
+    context_data = {
+        'title': title,
+        'description': description,
+        'form': form,
+        'template': template,
+    }
+    context_data['source'] = get_template_string(context_data['template'])
+    template_name = 'demoapp/demo.html'
+    return direct_to_template(request, template_name, context_data)
+
+
+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)

File demo/settings.py

View file
     # 'django.contrib.admin',
     # Uncomment the next line to enable admin documentation:
     # 'django.contrib.admindocs',
+    'djc.formrenderingtools',
+    'demoapp',
 )
 
 # A sample logging configuration. The only tangible logging

File demo/urls.py

View file
 
     # Uncomment the next line to enable the admin:
     # url(r'^admin/', include(admin.site.urls)),
+    url(r'', include('demoapp.urls', app_name='demoapp', namespace='demoapp')),
 )