本文实例讲述了Django框架实现的普通登录。分享给大家供大家参考,具体如下:
1.显示登录页面
a.设计url,通过浏览器访问http://127.0.0.1:8000//login的时候现实登录页面
b.设计url对应的视图函数
c.编写模板文件login.html
2.登录校验功能
校验数据库中有没有这个用户,这里用模拟的伪校验
新建login.html
在templates文件夹下的booktest文件夹下新建
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> POST:提交的参数在请求头里,数据比较重要用post GET:提交的参数在url中 用post方法提交到login_check页面中 <form method="post" action="/login_check"> 用户名:<input type="text" name="username"> 密码:<input type="password" name="password"> <input type="submit" value="登录"> </form> </body> </html>
注意这里表单的提交方法选择post方法,action配置如上
配置urls.py
url(r'^login$',views.login), url(r'^login_check$',views.login_check),
views.py写视图函数login()
和login_check()
login函数直接现实登录页面,login_check函数用request.POST.get()
函数接受浏览器传递过来的参数
def login(request): '''显示登录页面''' return render(request, 'booktest/login.html') def login_check(request): '''登录校验视图''' # 浏览器提交的信息就保存在request里面 # request.POST保存的是POST提交的参数 # request.GET保存的是GET提交的参数 # 1.获取提交的用户名和密码 username = request.POST.get('username') passwoed = request.POST.get('password') # 2.进行登录校验 # 实际开发的时候,用户名和密码保存在数据库中 # 模拟 if username == 'zhangyue' and passwoed == '123456': # 正确,跳转到首页index return redirect('/index') else: # 错误 return redirect('/login') # 3.返回应答
发生Forbidden (403)错误
去项目的setting.py里注释掉
'django.middleware.csrf.CsrfViewMiddleware',
希望本文所述对大家基于Django框架的Python程序设计有所帮助。
风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 群星.1993-滚石九大天王十二出好戏·贺岁齐唱【滚石】【WAV+CUE】
- 黑神话悟空上品虫校尉精魄获取方法一览|上品虫校尉精魄收集攻略
- 《指环王:夏尔的传说》推迟至2025年初发行
- 黑神话悟空上品蝎太子精魄获取方法一览|上品蝎太子精魄收集攻略
- 银霞.1977-《台北66电影原声带》台湾复刻版[WAV+CUE]
- 银霞.1980-《你那好冷的小手》台湾珍藏纪念版[WAV+CUE]
- 王菲.2006《菲常好听》引进版[低速原抓WAV+CUE]
- 《三日危情独家的记忆》女角色介绍
- 《天国拯救》拉泰比武大会攻略
- 任天堂推出《马里奥惊奇》NS捆绑包 10月24日发售
- 《孤岛惊魂》能否逆转育碧颓势?曝系列新作正招兵买马
- 天命人呼叫战术轰炸!玩家自制《黑神话:悟空》轰炸机Mod
- 群星.1992-滚石九大天王·十二出好戏【滚石】【WAV+CUE】
- 群星.1994-新滚石九大天王之年度大丫滚石】【WAV+CUE】
- 坣娜.1998-别恋【非常喜】【WAV+CUE】