Django的认证()返回永诺无 [英] django authenticate() allways returns None
问题描述
我想创建一个自定义登录表单进行身份验证对远程服务器,所以我必须创建一个自定义登录表单。我创建了一个与用户
用户名='TEST3,密码=测试在Django的壳。当我尝试使用身份验证功能,我拿到永诺无。当我试着使用相同的用户名和密码登录,我可以登录。我可以排除可能存在以下错误:
- django.contrib.auth.backends.ModelBackend'在MIDDLEWARE_CLASSES所示:
I'm trying to create a custom login-form for authenticating against a remote-server, so I have to create a custom login-form. I have created a user with username='test3', password='test' in the django-shell. When I try to use the authenticate-function, I allways get None. When I try to login with same username and password, I can login. I can exclude the following possible errors: - 'django.contrib.auth.backends.ModelBackend' is listed in MIDDLEWARE_CLASSES:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.auth.backends.ModelBackend',
)
- 密码存储哈希(我使用SQLite浏览器检查吧)
-
User.objects.get(用户名='TEST3')。check_password('测试')
返回True - 我没syncdb,迁移和激活认证后端后makemigrations
我使用的Django 1.8
原则上,我只需要会话数据,因为用户存储在不同的地方。
I'm using django 1.8 In principle, I only need the session-data because the users are stored in a different place.
今天早上,我才意识到,该问题不仅解决部分:
- 身份验证()的作品在shell
- 在我看来,我可以检索用户,并成功地检查其密码
但话又说回来,当我尝试使用身份验证() - 函数在我看来,我得到一个无类型对象。我甚至删除了数据库,但行为仍然是相同的。
我的settings.py:
This morning, I realized, that the problem is only solved partly: - authenticate() works in the shell - in my view I can retrieve the user and successfully check its password But then again, when I try to use the authenticate()-function in my view, I get a None-type object. I even deleted the database but the behaviour is still the same. My settings.py:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
#'django.contrib.auth.middleware.RemoteUserMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.contrib.staticfiles.finders.FileSystemFinder',
)
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
#'django.contrib.auth.backends.RemoteUserBackend',
)
的意见:
从django.contrib.auth.models导入用户
从django.contrib.auth进口身份验证
views: from django.contrib.auth.models import User from django.contrib.auth import authenticate
def authentifizieren(request):
"""
"""
if request.method == 'POST':
uname = request.POST['username']
passwd = request.POST['password']
dbuser = User.objects.get(username=uname)
dbuvalid=dbuser.check_password(passwd)
auser = authenticate(username=uname, password=passwd)
print('***************************************', dbuser,dbuvalid)
print('***************************************', auser)
if (auser != None):
login(request, auser)
return redirect('/startseite')
输出:
*************************************** test True
*************************************** None
我也很难codeD上的用户名/密码字符串exlude编码相关的问题,但没有成功。
I also hardcoded the user/password-strings to exlude an encoding-related problem, but no success.
推荐答案
<一个href=\"https://docs.djangoproject.com/en/1.8/ref/contrib/auth/#django.contrib.auth.backends.ModelBackend\"相对=nofollow> django.contrib.auth.backends.ModelBackend
是一个认证后端,而不是一个中间件类。
django.contrib.auth.backends.ModelBackend
is an authentication backend and not a middleware class.
您应该删除从 MIDDLEWARE_CLASSES
设置这个认证后端项目。
You should remove this authentication backend entry from MIDDLEWARE_CLASSES
settings.
此外,您还需要定义 AUTHENTICATION_BACKENDS
来类似如下:
Also, you need to define AUTHENTICATION_BACKENDS
to something like below:
# Authentication backends
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', # default
.. # any other authentication backends
)
这篇关于Django的认证()返回永诺无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!