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)