• cookie_test


    from django.shortcuts import render,redirect
    from app1 import models
    # Create your views here.
    def login(request):
        message = ""
        models.Administrator.objects.create(
            username = 'root',
            password= 'root'
        )
        if request.method == "POST":
            user = request.POST.get('user')
            pwd = request.POST.get('pwd')
            c =models.Administrator.objects.filter(username = user,password =pwd).count()
            if c:
                rep =redirect('/index')
                rep.set_cookie('username',user)
                return rep
            else:
                message = "mimaerror"
        return render(request,'login.html',{'msg':message})
    def index(request):
        # username = request.COOKIES.get('username')
        # if username:
        #      return render(request,'index.html',{'username':username})
        # else:
        #     return redirect('/login')
        username = "zhouying"
        return render(request,'index.html',{'username':username})

    django_view

    #login
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    
    </head>
    <body>
    
        <form action="login.html" method="post">
            {% csrf_token %}
            <div style="right:auto ">
                <label for="user">用户名: </label>
                <input id="user" type="text" name="user"/>
            </div>
            <div style="right: auto; ">
                <label for="pwd">密码: </label>
                <input id="pwd" type="password" name="pwd"/>
            </div>
            <div>
                <label> </label>
                <input type="submit" value="登陆"/>
                <span style="color: yellow">{{ msg }}</span>
            </div>
        </form>
    </body>
    </html>
    #index
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <h1 style="color: red">hello liuchao love {{username}}</h1>
    </body>
    </html>
    #url
    from django.conf.urls import url
    from django.contrib import admin
    from app1 import views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^login/', views.login),
        url(r'^index/', views.index),
    
    ]

    cookie(self,key,value='',max_age = None ,expires=None,path=‘/’,domain = None ,secure = False,httponly = False)

  • 相关阅读:
    C++多线程二
    C++多线程一
    定义抽象数据类型
    泛型函数
    关联容器(map):支持高效查找的容器,一种键值对的集合。
    字符串拆成单词的另一种实现
    将字符串拆成单词,并算最长的长度
    重载,排序,集合实例
    程序调用动态链接库中的方法,位图,类
    用bosybox制作文件系统
  • 原文地址:https://www.cnblogs.com/johnsonliu3/p/7985640.html
Copyright © 2020-2023  润新知