login.html文件

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Apollo</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
            integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
            crossorigin="anonymous"></script>
    <script>

    </script>
</head>
<body>
<div class="container">

    <form action="{% url 'login' %}" method="post">
        {% csrf_token %}
        <div class="col-md-6 col-md-offset-3">
            {# 输入账号 #}
            <div class="input-group">
                <span class="input-group-addon" id="sizing-addon2">账号</span>
                <input type="text" class="form-control" name="usr" placeholder="Username"
                       aria-describedby="sizing-addon2">
            </div>
            <br>

            {# 输入密码 #}
            <div class="input-group">
                <span class="input-group-addon" id="sizing-addon2">密码</span>
                <input type="text" class="form-control" name="pwd" placeholder="Password"
                       aria-describedby="sizing-addon2">
            </div>
            <br>

            {# 提交按钮 #}
            <button class="btn btn-default pull-right">提交</button>
        </div>
    </form>

</div>
</body>
</html>

index.html文件

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Apollo</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
            integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
            crossorigin="anonymous"></script>
    <script src="/static/jquery.js"></script>

</head>
<body>

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <a class="navbar-brand" href="javascript:void(0);">Apollo</a>
        <ul class="nav navbar-nav">
            <li class="active"><a href="javascript:void(0);">主页</a></li>
            <li><a href="javascript:void(0);">python</a></li>
            <li><a href="javascript:void(0);">html</a></li>
            <li><a href="javascript:void(0);">css</a></li>
            <li><a href="javascript:void(0);">javascript</a></li>
        </ul>
        <form class="navbar-form navbar-left" action="javascript:void(0);">
            <input type="text" placeholder="请输入关键词" class="form-control">
            <button type="submit">
                <span class="glyphicon glyphicon-search"></span>
            </button>
        </form>
        <a class="btn navbar-btn btn-primary btn-sm navbar-right" href="javascript:void(0);">免费下载</a>
        <div class="navbar-right">
            <ul class="nav navbar-nav">
                <li class="navbar-text">你好 {{ usr }}</li>
                <li><a class="navbar-link" href="{% url 'logout' %}">退出</a></li>
            </ul>

        </div>
    </div>
</nav>


</body>
</html>

urls.py文件

from django.contrib import admin
from django.urls import path
from coo import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('login/', views.login, name='login'),
    path('index/', views.index, name='index'),
]

views.py文件

from django.shortcuts import render, redirect, HttpResponse
from coo.models import UserInfo

# Create your views here.

# ############### COOKIES的login.html页面 ###############
def login(request):
    # 1 访问http://127.0.0.1:8080/login/
    # 2.根据请求方式分情况处理
    # 2.1 处理GET请求
    if request.method == 'GET':
        # 2.1.1返回login.html页面
        return render(request, 'login.html')
    # 2.2 处理POST请求
    else:
        # 2.2.1获取usr字段值
        usr = request.POST.get('usr')
        # 2.2.2获取pwd字段值
        pwd = request.POST.get('pwd')
        # 3验证信息
        # 3.1从数据库找与usr, pwd一样的字段
        user_obj = UserInfo.objects.filter(usr=usr, pwd=pwd).first()
        # 3.2分情况处理验证结果
        # 3.2.1验证通过
        if user_obj:
            # 验证通过,重定向到首页
            obj = redirect("/index/")
            # 设置cookie值,username:apollo
            obj.set_cookie("username", usr)
            # 设置cookie值,状态键值对:is_login:True,
            # 设置cookie值,时效键值对:max_age:3600(Second) * 24(Hour) * 7(Day),
            # 7(Day), 24(Hour), 60(Minutes), 60(Second)
            obj.set_cookie("is_login", True, max_age=3600 * 24 * 7)
            return obj
        # 3.2.2验证不通过
        else:
            # 验证不通过,返回错误提示.
            return HttpResponse('error')


# ############### COOKIES的index.html页面 ###############
def index(request):
    # 1.获取cookie值
    print(request.COOKIES)
    # 2.获取is_login的状态
    is_login = request.COOKIES.get("is_login")
    # 3.根据is_login情况分支处理
    # 3.1 当is_login值为False
    if not is_login:
        # 3.1.1重定向到登录页面,让用户先登录.
        return redirect("/login/")
    # 3.2 当is_login值为True
    else:
        # 此处username与设置set_cookie时写的名字保持一致
        # obj.set_cookie("username", usr)
        usr = request.COOKIES.get("username")

        return render(request, 'index.html', {'usr':usr})


# ############### COOKIES的logout ###############
def logout(request):
    request.session.flush()
    return redirect("/login/")