简体   繁体   English

使用 Django sqlite3 数据库登录时出现问题

[英]Issue in Login with Django sqlite3 database

I want to log in with the email that existed in the table named "user" in the sqlite3 database.我想使用 sqlite3 数据库中名为“user”的表中存在的 email 登录。 Whenever I am trying to find matching the emails as given photo.每当我试图找到与给定照片匹配的电子邮件时。 The "for" loop checking only the 1st email from the table of the database. “for”循环仅检查数据库表中的第一个 email。 What should I do to check all the emails that existed there and then log in when both are matched.我应该怎么做才能检查那里存在的所有电子邮件,然后在两者匹配时登录。 Please help me to solve it.请帮我解决它。

def login(request):
   form = forms.userform()
   if request.method == "POST":
          form = forms.userform(request.POST)         
          if form.is_valid():
                 email = form.cleaned_data['email'] 
                 for object in User.objects.all():
                     if object.email == email:
                      messages.success(request,'successfull login')
                      return users(request)

You can make use of authenticate() method, with this method you don't have to manually match the user with a for loop.您可以使用authenticate()方法,使用此方法您不必手动将用户与 for 循环匹配。

from django.contrib.auth import authenticate

def login(request):
   form = forms.userform()
   if request.method == "POST":
       form = forms.userform(request.POST)         
       if form.is_valid():
           email = form.cleaned_data['email']
           password = form.cleaned_data['password']

           user = authenticate(username=username, password=password')
           if user is not None:
               messages.success(request,'successfull login')
           else:
               messages.error(request,'Invalid Credentials')

   return render(request, "home.html")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM