Django管理员登录表单 - 覆盖max_length失败

时间:2022-12-04 20:04:49

I'm trying to make my admin login field greater than 30 characters because I'm using a custom Email Authentication backend that doesn't really care how long the username field is.

我正在尝试将我的管理员登录字段设置为大于30个字符,因为我使用的是自定义电子邮件身份验证后端,并不关心用户名字段的长度。

I wanted to set up a monkey_patch app that would apply the change to all admin sites.

我想设置一个monkey_patch应用程序,将应用程序更改到所有管理站点。

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

It's not working and I don't see why not.

它不起作用,我不明白为什么不。

The print statements in...

......中的印刷声明

  • django.contrib.admin.forms.AdminAuthenticationForm
  • django.contrib.admin.forms.AdminAuthenticationForm
  • django.contrib.auth.views.login
  • django.contrib.auth.views.login
  • django.contrib.auth.views.login.form # instantiated form
  • django.contrib.auth.views.login.form #instantiated form

... shows the correct, modified number when I render the login page via /myadmin/anywhere/.

...当我通过/ myadmin / anywhere /呈现登录页面时,显示正确的修改数字。

Even the form instance 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?

Where is the field magically deciding to be 30 chars long? I don't see where it has a chance to change between my print statement and render_to_response.

这个领域神奇地决定在30个字符长?我没有看到它有机会在我的print语句和render_to_response之间进行更改。

If I pass the admin site a subclassed AuthenticationForm, it works.

如果我通过管理站点传递子类AuthenticationForm,它的工作原理。

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

class MyAdmin(AdminSite):
     login_form = LongerAuthenticationForm

This is all confusing to me because I can see that the form instance passed to the final render function has the correct CharField with max_length=150.

这对我来说很困惑,因为我可以看到传递给最终渲染函数的表单实例具有正确的CharField,其max_length = 150。

3 个解决方案

#1


7  

It looks like I need to modify the widget attrs directly.

看起来我需要直接修改小部件attrs。

I forgot that fields are instantiated once!
CharField(max_length=30) is already setting the widget attributes for the HTML. No matter how I change max_length on a field instance, the widget has already been generated.

我忘记了字段被实例化了一次! CharField(max_length = 30)已经为HTML设置了widget属性。无论我如何更改字段实例上的max_length,都已生成窗口小部件。

Here's my solution in my monkey_patch app.

这是我在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

I don't really understand why instantiating a new field instance doesn't work..?

我真的不明白为什么实例化一个新的字段实例不起作用..?

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

#2


3  

In django 1.3 you can build an app with the following code and make sure you include it in the settings. Its a very similar solution to the accepted one but it extends from AdminAuthenticationForm, otherwise your non-field errors won't get displayed.

在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


1  

from django.contrib.admin.forms import AdminAuthenticationForm

来自django.contrib.admin.forms导入AdminAuthenticationForm

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

into urls.py

进入urls.py

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

...

admin.autodiscover()

#1


7  

It looks like I need to modify the widget attrs directly.

看起来我需要直接修改小部件attrs。

I forgot that fields are instantiated once!
CharField(max_length=30) is already setting the widget attributes for the HTML. No matter how I change max_length on a field instance, the widget has already been generated.

我忘记了字段被实例化了一次! CharField(max_length = 30)已经为HTML设置了widget属性。无论我如何更改字段实例上的max_length,都已生成窗口小部件。

Here's my solution in my monkey_patch app.

这是我在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

I don't really understand why instantiating a new field instance doesn't work..?

我真的不明白为什么实例化一个新的字段实例不起作用..?

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

#2


3  

In django 1.3 you can build an app with the following code and make sure you include it in the settings. Its a very similar solution to the accepted one but it extends from AdminAuthenticationForm, otherwise your non-field errors won't get displayed.

在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


1  

from django.contrib.admin.forms import AdminAuthenticationForm

来自django.contrib.admin.forms导入AdminAuthenticationForm

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

into urls.py

进入urls.py

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

...

admin.autodiscover()