Python Django身份验证返回None
更新:我已经缩小了问题的范围。我不知道为什么,Python Django身份验证返回None,python,django,authentication,nonetype,Python,Django,Authentication,Nonetype,更新:我已经缩小了问题的范围。我不知道为什么,authenticate()函数不起作用。以下是我在命令行中做的一些事情: C:\Python27\PROJECTS\Tutorial_sentdex\mysite>manage.py shell Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "l
authenticate()
函数不起作用。以下是我在命令行中做的一些事情:
C:\Python27\PROJECTS\Tutorial_sentdex\mysite>manage.py shell
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth import authenticate
>>> from users.models import UserProfile
>>> u = UserProfile.objects.get(username='red')
>>> u
<UserProfile: red>
>>> u.password
u'red'
>>> print authenticate(username=u.username,password=u.password)
None
>>>
models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class UserProfile(AbstractUser):
def __unicode__(self):
return self.username
设置.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '#nottelling'
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'users',
)
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.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS=(
os.path.join(BASE_DIR, "static"),
)
AUTH_USER_MODEL = 'users.UserProfile'
但是,我得到一个错误,即“NoneType”对象没有属性“is_active”。有人能帮我吗 您确定您的用户名和密码正确吗?您可以在管理员中检查用户名。 如果用户名或密码不正确,
auth.authenticate
将返回None。因此,您可能需要首先检查用户是否为None
e、 g.如您所引用的Django手册中所述--
如果user不是None并且user.is活跃:
您是否在Django的设置中将AUTH\u user\u模型设置为您的新模型
因为如果您正在为新模型设置用户,但没有使用AUTH_USER_model交换模型,则将无法进行身份验证并返回None
它将尝试在旧型号上验证您的登录名和密码。我发现了错误-Django自动对其用户密码进行编码,但我使用自定义表单创建用户配置文件。因此,密码编码不正确,我无法使用django的内置身份验证方法验证密码
如果任何人有类似的问题,只需在
Django.contrib.auth.forms
中使用Django提供的UserCreationForm
,您可以演示如何导入模块吗?另外,提供已安装的应用程序
,中间件类
和身份验证
设置(如果已定义)。谢谢。我更新了以显示如何导入模块和settings.py文件。我注意到我没有声明任何身份验证\u后端-它们有什么用途?这只意味着凭据是错误的。你确定用户名和密码是绝对正确的吗?是的,我已经仔细检查过了,并且我尝试了多个帐户。是的,我确保尝试了管理员-它也不能识别管理员。另外,我仔细检查了我在数据库中使用的测试用例,但仍然发现了错误。我面临着同样的问题,你能简要解释一下要修改哪行代码吗?我上次与Django合作已经有几年了,所以我无法给出确切的答案,但据我所知,问题是在使用Django内置的身份验证库时,您必须确保在用户创建和验证过程的每一步都使用Django的功能。我认为问题在于,我的用户创建表单的构造与常规的Post表单类似,但我应该使用官方的UserCreationForm
,因为这是以身份验证库期望的格式对给定帐户的信息进行编码的唯一方法。
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '#nottelling'
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'users',
)
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.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS=(
os.path.join(BASE_DIR, "static"),
)
AUTH_USER_MODEL = 'users.UserProfile'