我正在创建一种方法来登录用户。我构建了一个自定义表单和 View ,并让用户登录正常。但是,当我重定向到新页面时,当前的 request.user 会自动恢复为 AnonymousUser。我错过了什么?
log_in View 登录正常,我什至可以打印正确的 request.user。但是,一旦它重定向到 log_in_success 页面,它只会说 AnonymousUser 已登录!为什么没有在 View 之间保留登录用户?
def log_in(request):
if request.method == 'POST':
form = LogIn(request.POST)
if form.is_valid():
email = form.cleaned_data.get("email")
password = form.cleaned_data.get("password")
user = authenticate(email=email, password=password)
login(request, user)
print request.user.email
return HttpResponseRedirect('/log_in_success/')
else:
form = LogIn()
return render(request, 'events/log_in.html', {'form':form})
def log_in_success(request):
return HttpResponse("%s has logged in!" % request.user)
设置.py:
"""
Django settings for NHS project.
Generated by 'django-admin startproject' using Django 1.8.3.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
#AUTH_USER_MODEL = 'events.Student'
AUTHENTICATION_BACKENDS = ('events.backend.StudentBackend', )
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '=6b_2=!3s2hq3-nc@#rx6v=##u53xt!b=(#)c(2nk%&4qfpvy)'
# SECURITY WARNING: don't run with debug turned on in production!
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',
'events',
)
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 = 'NHS.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'NHS.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'nhs',
'USER': 'nhsadmin',
'PASSWORD': '#cbwestnhs',
'HOST': 'localhost',
'PORT': '',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/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/1.8/howto/static-files/
STATIC_URL = '/static/'
自定义身份验证后端:
from events.models import Student
from django.contrib.auth.hashers import check_password
class StudentBackend(object):
def authenticate(self, email=None, password=None):
stud = Student.objects.filter(email=email)
if stud.exists():
if check_password(password, stud[0].password):
return stud[0]
return None
def get_user(self, email):
stud = Student.objects.filter(email=email)
if stud.exists():
return stud[0]
else:
return None
表格.py:
from django import forms
from events.models import Student
from django.contrib.auth.hashers import check_password
class RegistrationForm(forms.ModelForm):
"""
Form for registering a new account.
"""
password1 = forms.CharField(widget=forms.PasswordInput,
label="Password")
password2 = forms.CharField(widget=forms.PasswordInput,
label="Password (again)")
class Meta:
model = Student
fields = ('firstname', 'lastname', 'email', 'password1', 'password2', 'is_second_year')
def clean(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
email = self.cleaned_data.get("email")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
if Student.objects.filter(email=email).exists():
raise forms.ValidationError("This email has already been registered")
return self.cleaned_data
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.set_password(self.cleaned_data['password1'])
if commit:
user.save()
return user
class LogIn(forms.Form):
email = forms.EmailField(label = "Email")
password = forms.CharField(widget=forms.PasswordInput, label="Password")
def clean(self):
password = self.cleaned_data.get("password")
email = self.cleaned_data.get("email")
stud = Student.objects.filter(email=email)
if (not (stud.exists())):
raise forms.ValidationError("This email has not been registered")
else:
if check_password(password, stud[0].password):
pass
else:
raise forms.ValidationError("Email and password combination do not match.")
return self.cleaned_data
最佳答案
我会尝试重新排列 context_processors
并将身份验证放在首位。如果这不起作用,您能否发布您的自定义身份验证后端?
设置.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Put auth on top
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.messages.context_processors.messages',
],
},
},
]
此外,我将进行以下更改以稍微清理您的 View :
View .py:
from django.contrib import messages
from django.core.urlresolvers import reverse
def log_in(request):
form = LogIn(request.POST or None)
if form.is_valid():
email = form.cleaned_data['email']
password = form.cleaned_data['password']
user = authenticate(email=email, password=password)
# May also help with your dilemma
if user is not None:
login(request, user)
# Be sure to change this to the appropriate template name
return HttpResponseRedirect(reverse('template_name'))
else:
messages.warning(request, "Username or password is incorrect.")
return render(request, 'events/log_in.html', {'form':form})
def log_in_success(request):
# "{}".format(value) is a more reliable string substitution
return HttpResponse("{} has logged in!".format(request.user))
- - - 编辑:
我会删除您的自定义后端并覆盖表单上的功能。它会为您省去很多麻烦。
class RegistrationForm(forms.ModelForm):
password1 = forms.CharField(widget=forms.PasswordInput(),
label="Password")
password2 = forms.CharField(widget=forms.PasswordInput(),
label="Password (again)")
class Meta:
model = Student
fields = ('firstname', 'lastname', 'email', 'password1', 'password2', 'is_second_year')
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
email = self.cleaned_data.get("email")
if password1 and password2:
if password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def clean_email(self):
email = self.cleaned_data["email"]
if email and Student.objects.filter(email=email):
raise forms.ValidationError("This email has already been registered")
return email
class LogIn(forms.Form):
email = forms.EmailField(label="Email")
password = forms.CharField(widget=forms.PasswordInput(), label="Password")
关于Python Django - 在 View 之间保持用户登录 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32105517/