Django框架实现的普通登录案例【使用POST方法】
作者:SpecYue 时间:2021-10-12 18:05:10
本文实例讲述了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程序设计有所帮助。
来源:https://blog.csdn.net/qq_34788903/article/details/87896645
标签:Django,登录,POST
0
投稿
猜你喜欢
ASP-server.URLEncode反函数:urldecode
2008-10-23 16:05:00
基于python和flask实现http接口过程解析
2022-06-01 11:46:36
微信支付的开发流程详解
2023-09-07 08:54:45
python读取与写入csv格式文件的示例代码
2023-08-09 09:07:15
python3.6中anaconda安装sklearn踩坑实录
2023-03-16 19:17:15
Python编程使用matplotlib绘制动态圆锥曲线示例
2021-08-30 03:38:18
PHP 应用容器化以及部署方法
2023-11-14 15:45:06
asp使用XMLHTTP下载远程数据输出到浏览器
2007-11-04 10:34:00
django使用图片延时加载引起后台404错误
2023-11-13 14:51:08
ASP 自动采集实现代码
2011-03-07 11:17:00
python 集合 并集、交集 Series list set 转换的实例
2023-12-16 10:53:34
CSS实现DIV完美垂直居中(支持多浏览器)
2007-08-13 09:21:00
趣味Python实战练习之自动更换桌面壁纸脚本附源码
2021-11-03 09:12:33
详解Python中的Lock和Rlock
2023-08-11 18:35:20
JavaScript代码着色器
2010-01-22 15:53:00
php防止sql注入代码实例
2023-08-15 21:17:21
python多线程超详细详解
2023-08-09 09:10:23
详解tensorflow载入数据的三种方式
2023-07-22 19:35:56
Dreamweaver使用技巧--让css使网页图片半透明
2010-09-05 21:13:00
Python超简单容易上手的画图工具库(适合新手)
2021-12-06 04:05:23