繁体   English   中英

Django身份验证未返回

[英]Django Authentication Returns None

更新:我已经缩小了问题所在。 我不知道为什么, 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很陌生,并且一直在使用个人资料登录系统。 我一直在使用Django Bookhttp://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'

但是,我得到一个错误,即“ NoneType”对象没有属性“ is_active”。 有人可以帮帮我吗?

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

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

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

例如,如您所引用的djangobook中所写- if user is not None and user.is_active:

您是否在Django的设置中将AUTH_USER_MODEL设置为新模型?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM