Python 如何检查自定义登录的用户名和密码
models.pyPython 如何检查自定义登录的用户名和密码,python,django,Python,Django,models.py class student(models.Model): u_email = models.CharField(max_length=50) u_username = models.CharField(max_length=50) u_password = models.CharField(max_length=20) view.py def login_check(request, *args,**kwargs ): if request.metho
class student(models.Model):
u_email = models.CharField(max_length=50)
u_username = models.CharField(max_length=50)
u_password = models.CharField(max_length=20)
view.py
def login_check(request, *args,**kwargs ):
if request.method == 'POST':
c_username= request.POST['username']
c_password = request.POST['password']
print(username,password)
如何在django中对authenticate student执行以下查询
从学生中选择id,其中u_username=c_username和password=c_password您可以使用并将其添加到您的代码中:
students = student.objects.filter(u_username=c_username, u_password=c_password)
if students.exists():
# do whatever you like with list students .e.g:
# access to first one id with students[0].id
不记得在视图中添加导入语句了吗
from models import student
def login_check(request, *args,**kwargs ):
if request.method == 'POST':
c_username= request.POST['username']
c_password = request.POST['password']
students = student.objects.filter(u_username=c_username, u_password=c_password)
if students:
print(students[0].id)
# ...
注意:请不要将密码保存为纯文本。使用哈希函数并保存其输出,请参见。你永远不需要知道用户的密码。您只需验证传入用户是否知道帐户的密码。从学生中选择id,其中u_username=c_username和u_password=c_password请不要在数据库中以纯文本形式存储密码!我会变得更强。你不能这样做。使用内置的身份验证框架,该框架负责确保密码正确。非常感谢您:回答我的问题,您不需要。存在-如果学生:足够。附言:不要鼓励以明文形式存储密码。附言:你怎么知道第一个学生是正确的?@thebjorn。谢谢你的建议。我更新了我的答案。@KaushalGajjar如果答案有效,请接受我的答案