[英]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.