Django用户身份验证和登录问题

时间:2020-09-29 10:26:41

标签: html django django-views django-templates

我创建了一个注册和登录表单。当我使用注册创建用户时,它可以正常运行并创建用户。但是,当我尝试使用同一用户登录时,无法登录。实际上,它转到else块并显示错误的凭据消息。

每当我尝试使用现有用户登录时,它都会显示错误的凭据。

Error message | Users data | Login window

view.py

    from django.shortcuts import render, HttpResponse, redirect
    from .models import Contact
    from django.contrib import messages
    from blog.models import Post
    from django.contrib.auth.models import User
    from django.contrib.auth import authenticate, login, logout
    
    def handleSignUp(request):
        if request.method == 'POST':
            username = request.POST.get('username')
            fname = request.POST.get('fname')
            lname = request.POST.get('lname')
            email = request.POST.get('email')
            pass1 = request.POST.get('pass1')
            pass2 = request.POST.get('pass2')
    
            #check verify sign field
            if len(username) > 10:
                messages.error(request, 'User name must not more than 10 characters')
                return redirect('/')
            if not username.isalnum():
                messages.error(request, 'User name must contain alpha numeric characters')
                return redirect('/')
            if pass1 != pass2:
                messages.error(request, 'Password did not match')
                return redirect('/')
            #create user
            myuser = User.objects.create_user(username, email, pass1)
            myuser.first_name = fname
            myuser.last_name = lname
            myuser.save()
            messages.success(request, 'You account has been Successfully created')
            return redirect('/')
        else:
            return HttpResponse('404 not found')
    
    def handleLogin(request):
        loginusername = request.POST.get('loginusername')
        loginpassword = request.POST.get('loginpassword')
        user = authenticate(request, username=loginusername, password=loginpassword)
        login(request, user)
        if user is None:
            messages.error(request, 'Wrong Credential, try again')
            return redirect('/')
        else:            
            messages.success(request, 'Your are sucessfully logged in')
            return redirect('/')
        return HttpResponse('handle login')
    def handleLogout(request):
        logout(request)
        messages.success(request, 'Sucessfully Log out')
        return redirect('/')

base.html

            {% if user.is_authenticate %}

            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown"
                    aria-haspopup="true" aria-expanded="false">
                    Welcome {{request.user}}
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                    <a class="dropdown-item" href="/logout">Logout</a>
                </div>
            </li>
            {% else %}

            <!-- Button trigger for signup modal -->
            <button type="button" class="btn btn-outline-success ml-2" data-toggle="modal" data-target="#signupModal">
                Sign Up
            </button>
            <!-- Button trigger for signup modal -->
            <button type="button" class="btn btn-outline-success ml-2" data-toggle="modal" data-target="#signinModal">
                Sign In
            </button>


            {% endif %}
        </div>
    </nav>
    <!-- this is for messages dissmissal bar -->
    {% for message in messages %}
    <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
        <strong>Message: </strong>{{ message }}
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    {% endfor %}
    <!-- ------------------------------------------------- -->
    <!--SignUp  Modal Start From Here -->
    <div class="modal fade" id="signupModal" tabindex="-1" aria-labelledby="signupModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="signupModalTitle">Sign Up Here</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form action="signup/" method="POST"> {% csrf_token %}
                        <div class="form-group">
                            <label for="username">User Name</label>
                            <input type="text" class="form-control" id="username"
                                placeholder="choose unique username (alpha numeric)" name="username" required>
                        </div>
                        <div class="form-group">
                            <label for="fname">First Name</label>
                            <input type="text" class="form-control" id="fname" placeholder="First Name" name="fname"
                                required>
                        </div>
                        <div class="form-group">
                            <label for="lname">Last Name</label>
                            <input type="text" class="form-control" id="lname" placeholder="Last Name" name="lname"
                                required>
                        </div>
                        <div class="form-group">
                            <label for="email">Email address</label>
                            <input type="email" class="form-control" id="email" placeholder="name@example.com"
                                name="email" required>
                        </div>
                        <div class="form-group">
                            <label for="pass1">Password</label>
                            <input type="password" class="form-control" id="pass1" name="pass1" required>
                        </div>
                        <div class="form-group">
                            <label for="pass2">Re-Enter Password</label>
                            <input type="password" class="form-control" id="pass2" name="pass2" required>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                            <button type="submit" class="btn btn-primary">Sign Up</button>
                        </div>
                        {% csrf_token %}
                    </form>
                </div>

            </div>
        </div>
    </div>
    <!-- ------------------------------------------------- -->
    <!--Sign IN  Modal Start From Here -->
    <div class="modal fade" id="signinModal" tabindex="-1" aria-labelledby="signinModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="signinModalTitle">Sign In Here</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form action="login/" method="POST">{% csrf_token %}
                        <div class="form-group">
                            <label for="loginusername">User Name</label>
                            <input type="text" class="form-control" id="loginusername" placeholder="loginusername
                                name=" loginusername" required>
                        </div>
                        <div class="form-group">
                            <label for="loginpassword">Password</label>
                            <input type="password" class="form-control" id="loginpassword" name="loginpassword"
                                required>
                        </div>

                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                            <button type="submit" class="btn btn-primary">Sign In</button>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>

    {% block body %}
    {% endblock %}

1 个答案:

答案 0 :(得分:0)

它不是user.is_authenticated,不是 user.is_authenticate

{% if user.is_authenticated %}

        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown"
                aria-haspopup="true" aria-expanded="false">
                Welcome {{request.user}}
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                <a class="dropdown-item" href="/logout">Logout</a>
            </div>
        </li>
{% else %}

编辑

似乎您在HTML中输入了错误的name属性。您的名字是 loginusername,应该是loginusername。所以:

 <div class="form-group">
     <label for="loginusername">User Name</label>
     <input name="loginusername" type="text" class="form-control" id="loginusername" placeholder="loginusername" required>
 </div>