Commits

Shu Zong Chen committed d3546ca

overriding betterform to actually display stuff set in fieldsets

Comments (0)

Files changed (5)

contactforms/fields.py

+from django import forms
+
+class TextAreaField(forms.CharField):
+	"""
+	form.CharField wrapper with textarea widget.
+	"""
+	widget = forms.Textarea

contactforms/forms.py

 		
 		fieldsets = []
 		for i, v in enumerate(data['fieldsets']):
-			fieldsets.append((i, v))
+			fieldsets.append((v.get('legend', str(i)).lower(), v))
 		base_form_fields['Meta'] = type('Meta', (), {'fieldsets': fieldsets})
 		
 		form = type(
 def yaml_to_field(data):
 	_type = data['type']
 	kwargs = dict((k,v) for k,v in data.items() if k not in ('id', 'type', 'autofocus'))
+	if 'required' not in kwargs:
+		kwargs['required'] = False
 	ret = FieldRegistry.get(_type, **kwargs)
 	if data.get('autofocus') is True:
 		ret.widget.attrs.update({'autofocus': 'autofocus'})

contactforms/models.py

 from django.template import RequestContext, Template, Context
 from django.template.loader import render_to_string
 from django.conf import settings
-from django import forms
+import django.forms
 import yaml
 import os
 from contactforms.forms import ContactForm, FieldRegistry
+import contactforms.fields
 
 class ContactModule(object):
 	"""
 	return module
 
 #associate Django's default fields in the field registry
-for i in dir(forms):
+for i in dir(django.forms):
 	if i.endswith('Field'):
-		FieldRegistry.register(i[:len(i)-5].lower(), getattr(forms, i))
+		FieldRegistry.register(i[:len(i)-5].lower(), getattr(django.forms, i))
 
+for i in dir(contactforms.fields):
+	if i.endswith('Field'):
+		FieldRegistry.register(i[:len(i)-5].lower(), getattr(contactforms.fields, i))

contactforms/templates/contactforms/better_form_override.html

+{% extends "form_utils/form.html" %}
+{% block fields %}
+{% for fieldset in form.fieldsets %}
+<fieldset class="{{ fieldset.classes }}">
+	{% if fieldset.legend %}
+	<legend>{{ fieldset.legend }}</legend>
+	{% endif %}
+	{% if fieldset.description %}
+	{{ fieldset.description }}
+	{% endif %}
+	<ul>
+		{% with fieldset as fields %}
+			{% for field in fields %}
+			{% if field.is_hidden %}
+			{{ field }}
+			{% else %}
+			<li{{ field.row_attrs }}>
+				{{ field.errors }}
+				{{ field.label_tag }}
+				{{ field }}
+				{% if field.help_text %}
+				<br>
+				<span>{{ field.help_text }}</span>
+				{% endif %}
+			</li>
+			{% endif %}
+			{% endfor %}
+		{% endwith %}
+	</ul>
+</fieldset>
+{% endfor %}
+{% endblock %}

contactforms/templates/contactforms/widget.html

 <h1>BLAAAAH</h1>
 <form method="POST" action=".">
 	{% csrf_token %}
-	{{ form|render }}
+	{{ form|render:"contactforms/better_form_override.html" }}
 	<input type="submit">
 </form>