[英]How do I login an existing user with Django's authentication system?
def sign_in(request):
#we need to handle all the data that was just typed, we'll add a condition for that
form = NameForm(request.POST)
if form.is_valid():
post = form.save()
post.save()
username = request.POST.get('username')
password = request.POST.get('password')
#auth = authenticate(username=username, password=password)
# If the username and password are provided try to auth them
if username and password:
print username, password
user = authenticate(username=username, password=password)
# if it authenticates successfully, check if the user is not an admin, log them in
if user:
if not user.is_staff:
login(request,user)
return HttpResponseRedirect('success')
else:
form = NameForm()
return render(request, 'checkin/sign_in_new.html', {'form': form})
编辑 :更改代码以与后端一起使用。 添加了user.is_staff
但是它仍然不会返回任何网页,只是停留在同一页面上
models.py
:
from __future__ import unicode_literals
from django.db import models
from django import forms
from django.forms import ModelForm
# Create your models here.
class Question(models.Model):
question_text = models.CharField("What is your ID?", max_length=100, null=True)
#pub_date = models.DateTimeField('date published')
id_text = models.CharField("", max_length=200, null=True)
def __str__(self):
return self.question_text
forms.py
:
from django.forms import ModelForm
from .models import Question
#put the form here
class NameForm(ModelForm):
class Meta:
model = Question
fields = ['question_text', 'id_text']
class IdForm(ModelForm):
class Meta:
model = Question
fields = ['id_text']
编辑2这些是我的模型和表单文件,这些文件不应该影响后端中某些参数的命名吗?
这就是我的操作方式,并且可以正常工作...但是我会自动创建用户,因此无需表单输入,但您应该了解一下:
user, created = User.objects.get_or_create(username=user_email, email=user_email)
if created:
secret = str(uuid4())
user.set_password(secret)
user.save()
user = User.objects.get(username=user_email)
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, user)
因此对于您来说,您可能只使用User.objects.get_or_create
...而不是create_user
并且您显然必须添加auth步骤。
Django身份验证系统。
我认为您熟悉使用模型保存用户数据的情况,可以使用身份验证后端来解决此问题,请参考以下步骤。 创建自定义后端。 在项目根后端/ staff_backends.py
from project_app_path.staffs.models import MODEL_NAME
from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User
class StaffBackend:
def authenticate(self, username=None, password=None):
try:
user = MODEL_NAME.objects.get(username=username)
if check_password(password, user.password):
return user
else:
return None
except MODEL_NAME.DoesNotExist:
return None
def get_user(self, user_id):
try:
return MODEL_NAME.objects.get(pk=user_id)
except MODEL_NAME.DoesNotExist:
return None
在项目设置中包含后端。
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'backends.staff_backends.StaffBackend',
]
在views.py中
from django.contrib.auth import authenticate, login ,logout
def any_view(request):
rg = request.POST.get
username = rg('username')
password = rg('password')
if username and password:
print username,password
user = authenticate(username=username,password=password)
if user:
if not user.is_staff:
login(request,user)
### Redirect where you want , you can use this code in to you signupage also.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.