Jerry 2020-05-28
urls.py
"""cookiesession URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r‘^$‘, views.home, name=‘home‘) Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r‘^$‘, Home.as_view(), name=‘home‘) Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r‘^blog/‘, include(‘blog.urls‘)) """ from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^index/‘, views.index, name=‘index‘), url(r‘^home/‘, views.home, name=‘home‘), # 登录 url(r‘^login/‘, views.login, name=‘login‘), ]
views.py
from django.shortcuts import render, HttpResponse, redirect # Create your views here. def loginAuth(func): def inner(request, *args, **kwargs): if request.COOKIES.get(‘isLogin‘) == ‘true‘: return func(request, *args, **kwargs) else: return redirect(‘login‘) return inner @loginAuth def index(request): return render(request, ‘index.html‘) @loginAuth def home(request): return render(request, ‘home.html‘) def login(request): if request.method == ‘GET‘: return render(request, ‘login.html‘) else: if request.POST.get(‘uname‘) == ‘xx‘ and request.POST.get(‘password‘) == ‘111‘: ret = redirect(‘home‘) ret.set_cookie(‘isLogin‘, ‘true‘) return ret else: return HttpResponse(‘登录失败‘) # def index(request): # print(type(request.COOKIES.get(‘isLogin‘))) # if request.COOKIES.get(‘isLogin‘): # return render(request, ‘index.html‘) # else: # return redirect(‘login‘) # def home(request): # is_login = request.COOKIES.get(‘k1‘) # if is_login == ‘v1‘: # return render(request, ‘home.html‘) # else: # return HttpResponse(‘login please!!!‘)
login.html
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --> <title>Bootstrap 101 Template</title> </head> <body> <h1>你好,请登录!</h1> <form action="{% url ‘login‘ %}" method="post"> {% csrf_token %} <label> 用户名:<input type="text" name="uname"> </label> <label> 密码:<input type="password" name="password"> </label> <button>登录</button> </form> </body> </html>
home.html
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --> <title>Bootstrap 101 Template</title> </head> <body> <h1>你好,home!</h1> </body> </html>
index.html
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --> <title>Bootstrap 101 Template</title> </head> <body> <h1>你好,index!</h1> </body> </html>