DRY in django.contrib.auth

0 views
Skip to first unread message

fest

unread,
Nov 4, 2009, 3:36:25 AM11/4/09
to Django users
Hello,
I'm not sure if I should post this message in django-developers or
here, but before I bother django developers, I wanted to discuss this
issue with django users.
I stumbled across this one when I was creating a custom User class, by
inheriting from django's built in User class. I extended User,
extended User admin, and even created a custom add form, but I just
couldn't get my form fields displayed. Having fair experience with
django forms, I thought, that these fields should automatically be
rendered in form. When I took a look at the form's code, I immediately
understood what's wrong with my approach- form fields were hardcoded
in django\contrib\admin\templates\admin\auth\user\add_form.html
Looking at the code, I see there is some duplication in
contrib.auth.forms file- help text and field names are both specified
in forms and in template- see yourself:

forms.py
class UserCreationForm(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given
username and password.
"""
username = forms.RegexField(label=_("Username"), max_length=30,
regex=r'^\w+$',
help_text = _("Required. 30 characters or fewer. Alphanumeric
characters only (letters, digits and underscores)."),
error_message = _("This value must contain only letters,
numbers and underscores."))
password1 = forms.CharField(label=_("Password"),
widget=forms.PasswordInput)
password2 = forms.CharField(label=_("Password confirmation"),
widget=forms.PasswordInput)


add_form.html
<p>{% trans "First, enter a username and password. Then, you'll be
able to edit more user options." %}</p>

<fieldset class="module aligned">

<div class="form-row">
{{ form.username.errors }}
{# TODO: get required class on label_tag #}
<label for="id_username" class="required">{% trans 'Username' %}:</
label> {{ form.username }}
<p class="help">{{ form.username.help_text }}</p>
</div>

<div class="form-row">
{{ form.password1.errors }}
{# TODO: get required class on label_tag #}
<label for="id_password1" class="required">{% trans 'Password' %}:</
label> {{ form.password1 }}
</div>

<div class="form-row">
{{ form.password2.errors }}
{# TODO: get required class on label_tag #}
<label for="id_password2" class="required">{% trans 'Password
(again)' %}:</label> {{ form.password2 }}
<p class="help">{% trans 'Enter the same password as above, for
verification.' %}</p>
</div>
</fieldset>
(I left out irrelevant parts of code)
Shouldn't the add_form.html be converted to something like:

{% for field in form %}
<div class="form-row">
{{ field.errors }}
{% trans field.label_tag %}:{{ field }}
<p class="help">{{ field.help_text }}</p>
</div>
{% endfor %}

In my opinion this approach is more flexible and avoids unneccesary
duplication, while maintaining the same functionality and appearance.

And my point is- should I post this message to django-developers (I
know, some of the developers should read this list too) or keep my
thoughts to myselft? :>


Reinis
Reply all
Reply to author
Forward
0 new messages