简体   繁体   English

Python Django,如何使用用户名(uname)或 email 作为登录凭据?

[英]Python Django, How I Can use username(uname) or email as a login credentials?

Python Django, How I Can use username(uname) or email as a login credentials? Python Django,如何使用用户名(uname)或 email 作为登录凭据? my python file are views,URLs,models,settings.py我的 python 文件是视图、URL、模型、settings.py

def loginpage(request): def 登录页面(请求):

if request.method=="POST":
    try:
        Userdetails=newacc.objects.get(email=request.POST['email'],pwd=request.POST['pwd'])
        print("Username=",Userdetails)
        request.session[ 'email']=Userdetails.email
        return render(request,'Logout.html')
    except newacc.DoseNotExist as e:
        messages.success(request,' Username / Password Invalid.')
return render(request,'Login.html')

You can work with a Q object to make a disjunction between email and uname :您可以使用Q object 在emailuname之间进行分离:

from django.db.models import Q

if request.method=="POST":
    try:
        Userdetails=newacc.objects.get(
            Q(email=request.POST['email']) | Q(uname=request.POST['email']),
            pwd=request.POST['pwd']
        )
        print("Username=",Userdetails)
        request.session[ 'email']=Userdetails.email
        return render(request,'Logout.html')
    except newacc.DoseNotExist as e:
        messages.success(request,' Username / Password Invalid.')
return render(request,'Login.html')

But it seems that your user model does not make use of password hashing .但是您的用户 model 似乎没有使用密码散列 I strongly advise to read the documentation on password management and hash passwords in your user model to prevent hackers from exploiting the passwords stored in the database if they manage to read that.我强烈建议您阅读用户 model 中有关密码管理和 hash 密码的文档,以防止黑客在设法读取数据库中存储的密码时利用这些密码。 See also this section on Customizing Authentication in Django .另请参阅Django 中的自定义身份验证部分

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用 django 中的额外字段(除用户名/ email 和密码之外)验证任何用户登录 - How can i authenticate any user for login with an extra field (other then username/ email and password) in django Django Registration Redux:如何将唯一标识符从用户名更改为电子邮件,并使用电子邮件作为登录名 - Django Registration Redux: how to change the unique identifier from username to email and use email as login 如何在函数Login()中获取'username'的值,以在另一个python程序上使用它? - How can I get the value of 'username' inside the function Login() to use it on another python program? Django - 使用电子邮件或用户名登录无效 - Django - Login with email or username not working 如何使用email作为Django注册的用户名 - How to use email as username for Django registration django关于使用电子邮件作为用户名登录的权限 - django about permission with email as username login 在 django 项目中允许电子邮件和用户名登录 - Allowing both email and username login in django project 如何将数据库中的用户名保存为 email?> - How can I save the username in the database as an email?> 我可以删除现有字段或在 Django 的默认用户数据库中添加更多字段吗?我还可以使用 email 而不是用户名登录吗? - Can I remove existing fields or add some more fields in the default User DB in Django?, Also can i login using email instead of username? 如何在Django用户名中允许空格? - How can I allow spaces in a Django username?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM