Add templatetags for form handling.
authorMadhusudan.C.S <madhusudancs@gmail.com>
Sun, 16 Jan 2011 17:02:02 +0530
changeset 424 5ed0362bd674
parent 423 966d3241d2b4
child 425 e15749fab2e4
Add templatetags for form handling.
pytask/settings.py
pytask/templates/templatetags/_as_div_field.html
pytask/templates/templatetags/_as_div_form.html
pytask/templatetags/__init__.py
pytask/templatetags/form_helpers.py
--- a/pytask/settings.py	Sun Jan 16 17:00:37 2011 +0530
+++ b/pytask/settings.py	Sun Jan 16 17:02:02 2011 +0530
@@ -93,6 +93,7 @@
     'django.contrib.sites',
     'django.contrib.messages',
     'django.contrib.admin',
+    'pytask',
     'pytask.profile',
     'pytask.taskapp',
 )
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templates/templatetags/_as_div_field.html	Sun Jan 16 17:02:02 2011 +0530
@@ -0,0 +1,11 @@
+{% load form_helpers %}
+
+{%if field.field.required %}
+<div id="field-required">
+{% else %}
+<div id="field">
+{% endif %}
+  {{ field.errors }}
+  {{ field.label_tag }}
+  {{ field }}
+</div>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templates/templatetags/_as_div_form.html	Sun Jan 16 17:02:02 2011 +0530
@@ -0,0 +1,12 @@
+{% load form_helpers %}
+
+<form action="{{ action_url }}" method="post">
+  {% csrf_token %}
+  <div id="form">
+    {% for field in form %}
+      {% as_div_field field %}
+    {% endfor %}
+    <p class="submit"><button type="submit">{{ button_label }}</button></p>
+  </div>
+</form>
+  
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templatetags/__init__.py	Sun Jan 16 17:02:02 2011 +0530
@@ -0,0 +1,2 @@
+"""Package containing templatetags used all across the site.
+"""
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytask/templatetags/form_helpers.py	Sun Jan 16 17:02:02 2011 +0530
@@ -0,0 +1,37 @@
+"""Module containing the templatetags for constructing forms.
+"""
+
+
+__authors__ = [
+    '"Madhusudan.C.S" <madhusudancs@fossee.in>',
+    ]
+
+
+from django import template
+
+
+register = template.Library()
+
+
+@register.inclusion_tag('templatetags/_as_div_form.html')
+def as_div_form(form, form_name, csrf_token, action_url, button_label):
+    """Returns a form to be constructed by the template specified.
+    """
+
+    return {
+      'form': form,
+      'form_name': form_name,
+      'csrf_token': csrf_token,
+      'action_url': action_url,
+      'button_label': button_label,
+    }
+
+
+@register.inclusion_tag('templatetags/_as_div_field.html')
+def as_div_field(field):
+    """Returns the field for each div form field.
+    """
+
+    return {
+      'field': field,
+      }