Django身份验证返回无

时间:2014-12-31 00:42:06

标签: 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 "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
>>>

我是Django的新手,并且一直致力于个人资料登录系统。我一直在使用 The Django Book http://www.djangobook.com/en/2.0/chapter14.html)作为参考,但试图对用户进行身份验证。我在书中使用了与我的视图函数基本相同的代码:

from django.shortcuts import render
from users.models import UserProfile
from django.views import generic
from users.forms import UserForm
from django.contrib import auth
from django.http import HttpResponse

def loginView(request):
    if request.method=='POST':
        print request.POST.get('username','')
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        user = auth.authenticate(username=username, password=password)
        if user.is_active:
            auth.login(request, user)
            print 'HURRAH'
    else:
        print 'no post'

    return render(request,'login.html',{})

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser

class UserProfile(AbstractUser):

    def __unicode__(self):
        return self.username

settings.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'

但是,我收到的错误是&#39; NoneType&#39;对象没有属性&#39; is_active&#39;。有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:2)

我发现错误 - Django自动编码其用户密码,但我使用自定义表单来制作用户个人资料。因此,密码编码不正确,我无法使用django的内置身份验证方法验证它们。

如果有人遇到类似问题,请在UserCreationForm中使用Django提供的django.contrib.auth.forms

答案 1 :(得分:0)

您确定自己的用户名和密码是否正确?您可以在管理员中查看用户名。 如果用户名或密码不正确,auth.authenticate将返回None。因此,您可能想先检查用户是否为“无”。

e.g。正如你所引用的djangobook中所写 - if user is not None and user.is_active:

答案 2 :(得分:0)

你是否在Django的设置中为你的新模型设置了AUTH_USER_MODEL?

因为如果您为新模型设置用户但未使用AUTH_USER_MODEL交换模型,则无法进行身份验证并返回None。

它会尝试在旧模型上验证您的登录名和密码。