Python 如何使用Django';登录现有用户;谁的认证系统?
编辑:更改代码以使用后端。添加了Python 如何使用Django';登录现有用户;谁的认证系统?,python,django,Python,Django,编辑:更改代码以使用后端。添加了user.is_staff,但它仍然不会返回任何网页,只是停留在同一个网页上 models.py: 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 = f
user.is_staff
,但它仍然不会返回任何网页,只是停留在同一个网页上
models.py
:
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})
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
:
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})
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
编辑2这些是我的模型和表单文件,这些文件是否会影响我后端中某些参数的命名?我就是这样做的,它可以工作。。。但我是自动创建用户的,因此没有表单输入,但您应该了解:
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']
因此,对于您来说,您可能只需要使用User.objects.get\u或\u create
。。。而不是create\u user
很明显,您必须添加auth步骤。我就是这样做的,它可以工作。。。但我是自动创建用户的,因此没有表单输入,但您应该了解:
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']
因此,对于您来说,您可能只需要使用User.objects.get\u或\u create
。。。而不是create\u user
显然,您必须添加auth步骤。Django身份验证系统 我认为您熟悉使用模型保存用户数据 您可以使用身份验证后端解决此问题。请参阅以下步骤。创建自定义后端。在项目根目录中 backends/staff_backends.py
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)
包括带有项目设置的后端
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
in views.py
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'backends.staff_backends.StaffBackend',
]
Django认证系统 我认为您熟悉使用模型保存用户数据 您可以使用身份验证后端解决此问题。请参阅以下步骤。创建自定义后端。在项目根目录中 backends/staff_backends.py
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)
包括带有项目设置的后端
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
in views.py
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'backends.staff_backends.StaffBackend',
]
好的,谢谢。我试试看。你能解释一下吗?(用户名=user\u email,email=user\u email)?我应该使用什么参数?@Amon因此,在我的情况下,我会从另一个数据库/应用程序自动创建Django用户
user\u email
来自于此。在您的情况下,您希望使用用户输入的用户名
,以获取或创建用户
。按照get\u或\u create
的工作方式,它首先查找与参数匹配的用户
,如果不存在,则使用这些属性创建该用户。所以你想要用户。获取或创建(用户名=用户名)
好的,谢谢,我会尝试一下,你能解释一下(用户名=用户电子邮件,电子邮件=用户电子邮件)
?我应该使用什么参数?@Amon因此,在我的情况下,我会从另一个数据库/应用程序自动创建Django用户user\u email
来自于此。在您的情况下,您希望使用用户输入的用户名
,以获取或创建用户
。按照get\u或\u create
的工作方式,它首先查找与参数匹配的用户
,如果不存在,则使用这些属性创建该用户。所以你想要User.get\u或\u create(username=username)
嘿,非常感谢!我把这一切都放进了我的项目中,但我遇到了一个问题,当我提交表单时,什么都没有发生。它没有加载我重定向到的任何页面。我认为问题在于它没有加载自定义后端,因为当我尝试登录到admin
网站时,我收到一个导入错误,告诉我没有名为backends.staff\u backends的模块。我的项目名是网站
,我把后端
放在那里。所以我有/website/backends/staff\u backends.py
只是为了明确它需要在我的项目根目录中,而不是我的应用程序文件夹中,对吗?在项目根目录中创建一个名为backends的文件夹。请确保您已在目录中添加init.py文件,然后创建staff\u backends.py并将代码放入该文件中,然后在settings.py文件中提到后端配置。这让管理网站正常工作,但我仍然有同样的问题与views.py。它不会加载其他页面。检查我编辑的帖子我还添加了我的models.py
和forms.py
文件,如果我的模型/字段的名称影响backends.py
中某些参数的命名?像(username=username
例如?嘿,非常感谢!我把所有这些都放到了我的项目中,但我遇到了一个问题,当我提交表单时,什么都没有发生。它没有加载我重定向到的任何页面。我想问题是它没有加载自定义后端,因为当我尝试登录管理
网站时,我得到了导入错误告诉我没有名为backends.staff\u backends的模块。我的项目名称是website
,这是我放置backends
的地方。所以我有/website/backends/staff\u backends.py
只是为了澄清一下,它需要在我的项目根目录中,而不是我的应用文件夹中,对吗?在项目根目录控制器中创建一个文件夹y调用backends请确保您已将init.py文件添加到目录中,然后创建staff_backends.py并将代码放入该文件中,然后在settings.py文件中提及后端配置。嘿,很抱歉回复太晚,我添加了init.py文件。这使管理站点正常工作,我仍然存在与views.py文件相同的问题h、 它不会加载另一页。检查我编辑的帖子我还添加了我的models.py
和forms.py
文件,我的模型/字段的名称是否会影响backends.py
中某些参数的命名?例如(username=username
?