简体   繁体   中英

Django user login authentication

I'm want to log a user in, in simple web site. i tried to create my own login authentication but it send this error:

AttributeError at /login/, 'QuerySet' object has no attribute 'pk'

this is the code (views.py):

...
from django.contrib.auth import login as login_module

def login(request):
if request.method == "POST":
    Login = LoginFrom(request.POST)
    if Login.is_valid():
        user = Login.cleaned_data
        if User.objects.filter(username=user['username']).exists():
            user_info = User.objects.filter(username=user['username'] , 
                                            password=user['password']).values('name')
            if user_info.exists():
                login_module(request, user_info)
                messages.success(request, 'welcome  {}'.format(user_info[0]['name'] ) , 'welcome_message')
            else:
                messages.error(request, 'wrong password', 'password_message')
        else:
            messages.error(request, 'username doesn\'t exist', 'username_message')    
else:
    Login = LoginFrom()
return render(request, 'login.html')

then i replace it with this code that's from django document; it get me into another problem which is always return 'None':

views.py

...
...
from django.contrib.auth import authenticate
from django.contrib.auth import login as login_module

def login(request):
if request.method == "POST":
    Login = LoginFrom(request.POST)
    if Login.is_valid():
        cd = Login.cleaned_data
        user = authenticate(request, username = cd['username'], password = cd['password'])
        if user is not None:
            login_module(request, user)
            messages.success(request, 'login', 'welcome_message')
        else:
            messages.error(request, 'error (none)', 'error_message')
else:
    Login = LoginFrom()
return render(request, 'login.html')

(form.py) in both way:

class LoginFrom(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)

login.html:

      <!DOCTYPE html>
     <html>
        <head><title>login</title></head>
        {% include 'header.html' %}
        <body class="background">
           <form id="login_form" action="" method="POST"> 
              {% csrf_token %}     
              <p class="lead">login to your account</p> 
              <div class="form-group">
                 <br>
                 <div class="input-group">
                    <div class="input-group-text" id="basic-addon1"><i class="bi bi-people"></i></div>
                    <input type="text" name="username"  class="form-control" placeholder="Enter Your Username"required>
                 </div>  
                 <div>
                    {% if messages %}
                       {% for message in messages %}        
                          {% if 'username_message' in message.tags %}             
                             <div class="text-danger" > {{message}} </div> 
                          {% endif %}
                       {% endfor %} 
                    {% endif %}
                 </div>
              </div>  
                       
              <div class="form-group">
                 <br>
                 <div class="input-group">
                    <div class="input-group-text" id="basic-addon1"><i class="bi bi-key"></i></div>
                    <input type="password"  name="password" class="form-control" placeholder="Enter Your password" required>
                 </div>
                 <div>
                    {% if messages %}
                       {% for message in messages %}        
                          {% if 'password_message' in message.tags %}             
                             <div class="text-danger" > {{message}} </div> 
                          {% endif %}
                       {% endfor %} 
                    {% endif %}
                 </div>
              </div>

              <br>
              <button class="btn btn-outline-secondary" type="submit" name="login" value="login" >Login</button>
              <div>
                 {% if messages %}
                    {% for message in messages %}        
                       {% if 'welcome_message' in message.tags %}             
                          <div class="text-warning text-center" > {{message}} </div> 
                       {% endif %}
                    {% endfor %} 
                 {% endif %}
              </div>
           </form>
        </body>
     </html>

did i missed some part or should set some setting?

In your first solution,

password=user['password']

will always fail as the password saved in default auth_user table will have the password hashed.

I tried the second solution and it is working fine. That login view cannot return None because the only return statement is returning render method.

Can you please share the login.html template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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