5 ms·
The above solution is just plain ugly, HTML in Python. Another PITA, where Django templates just fall down, is forms. Suppose you have a form: class MyForm
by zeemonkee 16y ago
The above solution is just plain ugly, HTML in Python.
Another PITA, where Django templates just fall down, is forms. Suppose you have a form:
class MyForm(forms.Form):
name = forms.CharField()
You then add that in the template:
{{ form.name }}
All well and good; but what if you want to change the size, maxlength, or add a class ? These things clearly belong in the template, but no, you have to either add them in the form declaration:
name = forms.CharField(widget=forms.TextInput(attrs={'class' : 'foo'}))
or in the view:
form.fields['name'].widget.attrs = {'class' : 'foo'}
Both of which clearly break the separation of concerns. yes, you could write a template tag to get around this; but it's not in standard Django.
Contrast the following in Jinja2 (using WTForms, but you get the idea):
{{ form.name(class_="foo") }}
- simonw 16y agoI agree that this is pretty frustrating - it's one of the things my django-html package addresses: http://github.com/simonw/django-html http://github.com/simonw/django-html {% field form.name class="myclass" %}
- zeemonkee 16y agoI had a similar workaround in a project, it did something like this: {% form.name|attrs:"class='foo', maxlength=20" %} If Django templates allowed function calls however (as Jinja2 does) that would eliminate the need for a lot of these tags.
- metamemetics 16y ago>All well and good; but what if you want to change the size, maxlength, or add a class ? Adding a class: Django gives every element of the form id values for easy styling in CSS. Just do {{form.as_p}} and see what it outputs. size and max length: I'm fine with these in python as they correspond to the max-length that would be validated into a data member. If you mean pixel size on screen, this belongs neither in HTML or your template but in CSS, since browsers render forms completely differently regardless of their HTML attributes, you have to style their width\height in CSS to get anything consistent.
- erlanger 16y agoUnfortunately, the defaults stink. 1.2 remedies this for error messages at least. as_p and co. should include some class describing the field type in the outer <p> tag so that a developer can style the label and error messages in a way specific to the field type.