6.2 Implement user login logic

Write the login function in views.py of the users module, and then reference this function in urls.py to implement the login logic.

users > views.py code:

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login


# Create your views here.


def user_login(request):
    if request.method == 'GET':
        return render(request, 'login.html', {})
    if request.method == 'POST':
        user_name = request.POST.get('username', '')
        pass_word = request.POST.get( ' password ' , '' )
         #successfully returns user, failure returns None 
        user = authenticate(username=user_name, password= pass_word)
         if user is  not None:
            login(request, user)   #Direct login 
            return redirect( ' home ' )
         else :
             return render(request, ' login.html ' , { ' msg ' : ' The username or password is incorrect. ' })

 

Code in urls.py:

from django.contrib import admin
from django.urls import path
import xadmin
from django.views.generic import TemplateView
from users.views import user_login

urlpatterns = [
    path('xadmin/', xadmin.site.urls),
    path('', TemplateView.as_view(template_name='index.html'), name='home'),
    # path('login/', TemplateView.as_view(template_name='login.html'), name='login'),
    path('login/', user_login, name='login'),
]

Modify the login section in index.html and add a login judgment:

 

                <div class="wp">
                    <div class="fl"><p>服务电话:<b>33333333</b></p></div>
                    <!--Jump after login--> 
                    { % if request.user.is_authenticated % }
                         <div class = " personal " >
                            <dl class="user fr">
                                <dd>bobby<img class="down fr" src="/static/images/top_down.png"/></dd>
                                <dt><img width="20" height="20" src="/static/media/image/2016/12/default_big_14.png"/>
                                </dt>
                            </dl>
                            <div class="userdetail">
                                <dl>
                                    <dt><img width="80" height="80"
                                             src="/static/media/image/2016/12/default_big_14.png"/>
                                    </dt>
                                    <dd>
                                        <h2>django</h2>
                                        <p>bobby</p>
                                    </dd>
                                </dl>
                                <div class="btn">
                                    <a class = " personcenter fl " href= " usercenter-info.html " >Enter personal center</a>
                                    <a class="fr" href="/logout/">退出</a>
                                </div>
                            </div>
                        </div>
                    {% else %}
                        <a style="color:white" class="fr registerbtn" href="/register/">注册</a>
                        <a style="color:white" class="fr loginbtn" href="/login/">登录</a>
                    {% endif %}


                </div>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325214030&siteId=291194637