Django login form - exceeding max_length

I am trying to make admin login field more than 30 characters, because I use my own email authentication server, which really does not care about how long the username field will be.

I wanted to create an application monkey_patchthat would apply the change to all admin sites.

from django.contrib.auth.forms import AuthenticationForm
AuthenticationForm.base_fields['username'].max_length = 150 # or whatever

This does not work, and I do not understand why not.

Print statements in ...

  • django.contrib.admin.forms.AdminAuthenticationForm
  • django.contrib.auth.views.login
  • django.contrib.auth.views.login.form # instance of the form

... shows the correct, changed number when I put out the login page through /myadmin/anywhere/.

Even an instance of the form in the final render function shows the correct number.

# django.contrib.auth.views.login
...
print form.fields['username'].max_length # this is an instantiated form!
return render_to_response(template_name, context ...)

What am I missing?

, 30 ? , render_to_response.

AuthenticationForm, .

class LongerAuthenticationForm(AuthenticationForm):
    username = forms.CharField(max_length=150)

class MyAdmin(AdminSite):
     login_form = LongerAuthenticationForm

, , , , CharField max_length=150.

+3
3

, attrs.

, ! CharField(max_length=30) HTML. , max_length , .

monkey_patch .

from django.contrib.auth.forms import AuthenticationForm

AuthenticationForm.base_fields['username'].max_length = 150 # I guess not needed
AuthenticationForm.base_fields['username'].widget.attrs['maxlength'] = 150 # html
AuthenticationForm.base_fields['username'].validators[0].limit_value = 150

, .?

AuthenticationForm.base_fields['username'] = forms.CharField(max_length=100) 
+7

django 1.3 , . , AdminAuthenticationForm, .

from django.contrib.admin.forms import AdminAuthenticationForm
from django import forms
from django.contrib.admin.sites import AdminSite

class LongerAuthenticationForm(AdminAuthenticationForm):
    """ Subclass which extends the max length of the username field. """
    username = forms.CharField(max_length=150)


AdminSite.login_form = LongerAuthenticationForm
+3

from django.contrib.admin.forms import AdminAuthenticationForm

class ModifiedForm(AdminAuthenticationForm):
    username = forms.CharField(max_length=150) #and so on

in urls.py

from django.contrib import admin
admin.site.login_form = ModifiedForm

...

admin.autodiscover()
+1
source

All Articles