django 快速实现完整登录系统
2016-09-03 00:51
288 查看
django 实现完整登录系统
本操作的环境:
===================
Windows 7 64
python3.5
Django 1.10
===================
创建项目与应用 :
#创建项目
django-admin.py startproject mysite5 cd mysite5 #在项目下创建一个online应用 python manage.py3.5 startapp online
目录结构如下:
from django.shortcuts import render # Create your views here. #coding=utf-8 from django.shortcuts import render,render_to_response from django.http import HttpResponse,HttpResponseRedirect from django.template import RequestContext from django import forms from online.models import User #表单 class UserForm(forms.Form): username = forms.CharField(label='用户名',max_length=100) password = forms.CharField(label='密码',widget=forms.PasswordInput()) #注册 def regist(req): if req.method == 'POST': uf = UserForm(req.POST) if uf.is_valid(): #获得表单数据 username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] #添加到数据库 User.objects.create(username= username,password=password) return HttpResponse('regist success!!') else: uf = UserForm() return render_to_response('regist.html',{'uf':uf}, ) #登陆 def login(req): if req.method == 'POST': uf = UserForm(req.POST) if uf.is_valid(): #获取表单用户密码 username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] #获取的表单数据与数据库进行比较 user = User.objects.filter(username__exact = username,password__exact = password) if user: #比较成功,跳转index response = HttpResponseRedirect('/online/index/') #将username写入浏览器cookie,失效时间为3600 response.set_cookie('username',username,3600) return response else: #比较失败,还在login return HttpResponseRedirect('/online/login/') else: uf = UserForm() return render_to_response('login.html',{'uf':uf},) #登陆成功 def index(req): username = req.COOKIES.get('username','') return render_to_response('index.html' ,{'username':username}) #退出 def logout(req): response = HttpResponse('logout !!') #清理cookie里保存username response.delete_cookie('username') return responseviews.py 这里实现了所有注册,登陆逻辑,中间用到cookie创建,读取,删除操作等。
创建模板
先在mysite5/online/目录下创建templates目录,接着在mysite5/online/templates/目录下创建regist.html 文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>注册</title> </head> <body> <h1>注册页面:</h1> <form method = 'post' enctype="multipart/form-data"> {% csrf_token %} {{uf.as_p}} <input type="submit" value = "ok" /> </form> <br> <a href="http://127.0.0.1:8000/online/login/">登陆</a> </body> </html>
mysite5/online/templates/目录下创建login.html 文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>登陆</title> </head> <body> <h1>登陆页面:</h1> <form method = 'post' enctype="multipart/form-data"> {% csrf_token %} {{uf.as_p}} <input type="submit" value = "ok" /> </form> <br> <a href="http://127.0.0.1:8000/online/regist/">注册</a> </body> </html>
mysite5/online/templates/目录下创建index.html 文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title></title> </head> <body> <h1>welcome {{username}} !</h1> <br> <a href="http://127.0.0.1:8000/online/logout/">退出</a> </body> </html>
设置模板路径
打开mysite5/mysite5/settings.py文件,在底部添加:
STATICFILES_DIRS = ( os.path.join(BASE_DIR,'online/templates'), )
使用功能 :
注册成功,提示“regist success!!”
登陆
执行登陆操作,通过读取浏览器cookie 来获取用户名
登录成功
参考链接:http://www.cnblogs.com/fnng/p/3750596.html(虫师)
相关文章推荐
- django 快速实现完整登录系统
- django 快速实现完整登录系统(cookie)
- django快速实现完整登录系统,把登陆注册串在一起并增加cookie(六)
- django 快速实现完整登录系统(cookie)
- django 快速实现完整登录系统(cookie)
- 04.Django实现完整登录系统的两种方法(cookie and session)
- DjangoWeb: 快速实现注册登录
- Win7快速实现自动登录系统
- Django admin实现图书管理系统菜鸟级教程完整实例
- django 快速实现登录
- 利用系统自带工具快速实现SSH免密远程登录
- python django 快速实现注册,登录,注销
- django快速实现登录
- django 快速实现登录
- ATM系统实现[1]——用户登录界面[00原创]
- ext实现完整的登录代码
- 利用现有资源快速实现汉语专用分词系统
- 同时实现同时只允许一个人登录系统
- 用Spring MVC实现用户登录的完整实例
- asp 实现同一帐号,防止多人同时登录、获取在线列表、实现登录日志(可用户统计在线时长),适合用户访问量大的系统