标签:form eth you 生成 put def 字符串 end res
1. 搭建环境请参考:http://www.cnblogs.com/momo8238/p/7508677.html
2. 写了login.html, index.html 2个页面备用
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/login/" method="POST" > <input type="text" name="user" placeholder="user"/> <input type="text" name="pwd" placeholder="pwd"/> <input type="submit" value="提交"/> </form> </body> </html>
views.py
from django.shortcuts import render,HttpResponse,redirect # Create your views here. def login(request): if request.method==‘GET‘: return render(request,‘login.html‘) elif request.method==‘POST‘: user=request.POST.get(‘user‘) pwd=request.POST.get(‘pwd‘) if user==‘root‘ and pwd==‘123‘: return redirect(‘/index/‘) else: return render(request,‘login.html‘) def index(request): return HttpResponse(‘OK‘)
此时没有加任何验证信息,所以直接访问index页面也是可以的。无需登录
3. 增加验证信息
3.1 说明:
在Django内部运行session之前,一定要先运行以下两句生成表。
python manage.py makemigrations
python manage.py migrate
3.2 运行server,如果用户名和密码正确,在浏览器端就能生成Cookies
同时,在服务器端生成了session,Django里面的session默认是保存在数据库中的。打开数据库就可以看到了。
3.3 用户相关的信息都放到request.session里面了。如果登录成功,就在index页面显示用户名。
from django.shortcuts import render,HttpResponse,redirect # Create your views here. def login(request): if request.method==‘GET‘: return render(request,‘login.html‘) elif request.method==‘POST‘: user=request.POST.get(‘user‘) pwd=request.POST.get(‘pwd‘) if user==‘root‘ and pwd==‘123‘: #在session里面设置值 request.session[‘username‘]=user request.session[‘is_login‘]=True return redirect(‘/index/‘) else: return render(request,‘login.html‘) def index(request): #获取当前用户的随机字符串 #根据随机字符串获取对应的信息 #去session中获取值,如果登录成功,显示用户名 if request.session[‘is_login‘]: return HttpResponse(request.session[‘username‘]) else: return HttpResponse(‘滚‘)
效果:
程序粘贴:
urls.py
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^login/$‘, views.login), url(r‘^index/$‘, views.index), ]
views.py
from django.shortcuts import render,HttpResponse,redirect # Create your views here. def login(request): if request.method==‘GET‘: return render(request,‘login.html‘) elif request.method==‘POST‘: user=request.POST.get(‘user‘) pwd=request.POST.get(‘pwd‘) if user==‘root‘ and pwd==‘123‘: #在session里面设置值 request.session[‘username‘]=user request.session[‘is_login‘]=True return redirect(‘/index/‘) else: return render(request,‘login.html‘) def index(request): #获取当前用户的随机字符串 #根据随机字符串获取对应的信息 #去session中获取值,如果登录成功,显示用户名 if request.session[‘is_login‘]: return HttpResponse(request.session[‘username‘]) else: return HttpResponse(‘滚‘)
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/login/" method="POST" > <input type="text" name="user" placeholder="user"/> <input type="text" name="pwd" placeholder="pwd"/> <input type="submit" value="提交"/> </form> </body> </html>
标签:form eth you 生成 put def 字符串 end res
原文地址:http://www.cnblogs.com/momo8238/p/7700035.html