如何修改Django的认证模块?
我对 Django 比较陌生,并且正在尝试对 Django 的核心身份验证模块进行一些更改。我添加了一些新代码,根据用户的电子邮件地址对 django.contrib.auth.backends.py< 中的
ModelBackend
类的 authenticate
方法进行身份验证。 /code>,但是这个新代码似乎没有任何效果,即使我将以下内容添加到我的设置中也是如此。
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
事实上,我删除了整个 authenticate
方法,并且仍然能够正常登录我的 Django 应用程序。有人知道我哪里出错了,最好的方法是修改 Django 的核心身份验证系统吗?
I'm relatively new to Django, and am trying to make some changes to Django's core authentication module. I added some new code that authenticates a user based on their email address to the authenticate
method of the ModelBackend
class in django.contrib.auth.backends.py
, however this new code doesn't seem to have any effect, even when I added the below to my settings.
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
In fact I deleted the entire authenticate
method and was still able to log into my Django application just fine. Anyone know where I'm going wrong here, and what the best way is to revise Django's core authentication system?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将
django.contrib.auth.backends.ModelBackend
后端导入到模块中,对其进行子类化,在进程中覆盖authenticate
,然后指向AUTHENTICATION_BACKENDS
在你的子类中。请参阅编写自己的身份验证后端 在手册中。事实上,您应该阅读整页。不要直接更改 django.contrib 中的任何内容。下次升级时它将被震撼。将其子类化或寻找挂钩它的方法。
当你把东西撕下来时,身份验证起作用了,我并不感到惊讶。 Django 的身份验证是一个附加组件; Django 需要能够在没有它的情况下工作,因此默认值为“允许”。
You are supposed to import the
django.contrib.auth.backends.ModelBackend
backend into a module, subclass it, overridingauthenticate
in the process, and then pointAUTHENTICATION_BACKENDS
at your subclass. See the section on writing your own authentication backend in the manual. In fact, you should read the whole page.Don't alter anything in
django.contrib
directly. It will get blown away with the next upgrade. Subclass it or look for ways to hook into it.I'm not surprised authentication worked when you ripped things out. Django's authentication is an add-on; Django needs to be able to work without it, so the default will be "allow".