python_Django 实现登入功能form表单的参数接收处理
1.创建Django工程。
参考https://www.cnblogs.com/CK85/p/10159159.html中步骤。
2.在urls.py文件中添加url分发路径
"""Django_test URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app01 import views urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.login, name='login'),
path('authentication/', views.authentication, name='authentication'),
]
路径分发时编辑name可以为路径取一个别名,在html文件中通过别名进行访问,避免过多的命名的改变。
3.在view.py中添加两个界面的试图函数。
from django.shortcuts import render, HttpResponse
import time # Create your views here. def login(request):
return render(request, 'login.html', locals()) def authentication(request):
# print(request.method)
user_name = request.GET.get('user')
pwd = request.GET.get('pwd')
user_dic = {
'ck': '123',
'ck1': '123',
'ck2': '123',
}
if user_name in user_dic:
if user_dic[user_name] == pwd: print('authentication passed')
user = user_name
output = render(request, 'well_come.html', {'username': user})
else:
print('incorrect username or password')
output = HttpResponse('<h1>incorrect username or password</h1>') else:
print('不在')
output = HttpResponse('<h1>the user dose not exist</h1>') return output
login视图函数在用户访问时将login.html发送给用户。
authentication进行用户登入信息的验证和处理。
HttpResponse返回一个http响应。
4.在templates文件夹中创建对应的html文件:
login.html:
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8">
<title>Title</title> </head>
<body>
<h1>登入</h1>
<div>
<form action="{% url "authentication" %}" >
<p>手机号:<input type="text" name="user"></p>
<p>密码:<input type="text" name="pwd"></p>
<p><input type="submit" value="提交"></p>
{% csrf_token %}
</form>
</div>
</body>
</html>
login界面是用户在登入时看到的界面,需要用户在该页面中输入账号和密码。
其中使用form表单进行读取数据,action=“/authentication/”与action=“{% url "authentiction" %}”相同,前者使用路径访问,后者使用别名访问。
在form表单中input标签type=’text‘ 和type=’submit‘组合使用,使得在用户点击提交按钮时使用form表单method中传入的方法将两个text中的值与text的name组成键值对传给在form表单action路径的文件。
well_come.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>你好! {{ username }}</h1>
</body>
</html>
well_come界面为用户成功登入之后的欢迎界面。
python_Django 实现登入功能form表单的参数接收处理的更多相关文章
- form 表单 action 参数 接收不了
<form method="get" action="/test/index.php?mod=123456" > <input type=&q ...
- 在IOS设备上POST提交form表单,后台接收不到值怎么办?
原文:https://blog.csdn.net/xhaimail/article/details/90440029 最近在工作上遇到一个奇葩问题,在Android和Windows平台上做请求时参数都 ...
- Web---演示Servlet的相关类、表单多参数接收、文件上传简单入门
说明: Servlet的其他相关类: ServletConfig – 代表Servlet的初始化配置参数. ServletContext – 代表整个Web项目. ServletRequest – 代 ...
- Form 表单提交参数
今天因为要额外提交参数数组性的参数给form传到后台而苦恼了半天,结果发现,只需要在form表单对应的字段html空间中定义name = 后台参数名 的属性就ok了. 后台本来是只有模型参数的,但是后 ...
- 前端通过form表单构造带参数url
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- select空间提交form表单传递参数
如下, 到了 <form name="modelForm" action="/portal/defectinfo/toDefectPage?projectname= ...
- form表单提交参数封装
function getFormValues(element,options) { var data = {}; if(element == null || element == undefined) ...
- springmvc使用map接收form表单的参数
其实只需要在map前面加上@RequestParam参数即可,jsp的name等都不变 public String queryByCondition(@RequestParam Map<Stri ...
- thinkPHP form表单提交参数无法获取
后台打印获取的数据为empty, 找了半天,是因为 input标签没有写name, 真是醉了!记一下,免得以后再犯错了.
随机推荐
- 版本控制(.git + .svn)
git 分布式版本控制系统 底层C语言 按元数据方式存储,采用SHA-1哈希算法(内容完整性好) 结合GitHub,为开源项目免费提供Git存储 git config --global user.na ...
- Lucene7.4学习和简单使用
简述: 前面从新回顾学习了Solr,正好也借此机会顺便学习一下Lucene. 一.什么是Lucene? 全文检索的一个实现方式,也是非结构化数据查询的方法.应用场景:在数据量大,数据结构不固定的时候, ...
- node-redis模块需要注意的事项
node之中连接redis使用的redis模块,虽然好用,但是有些地方还是需要注意. npm install redis redis client 行为:1.客户端执行过程中断网的情况 由于原本连接正 ...
- C#-进制转换、基础语句、语句的总结与练习——★for循环:九九乘法表、三角形、菱形★
//for循环嵌套练习——打一个九九乘法表 ; i <= ; i++) { ; j <= i; j++) { Console.Write(j + "×" + i + & ...
- #Go# 点滴积累
此篇仅为不断记录趟过的坑 StringToTimestamp import ( "time" ) const TimeFormat = "2006-01-02T15:04 ...
- HDU - 6315 吉司机线段树
题意:给出a,b数组,区间上两种操作,给\(a[L,R]\)+1s,或者求\(\sum_{i=l}^{r}a_i/b_i\) 一看就知道是吉司机乱搞型线段树(低配版),暴力剪枝就好 维护区间a的最大值 ...
- swagger注释API详细说明
API详细说明 注释汇总 @RequestMapping此注解的推荐配置 value method produces 示例: @ApiOperation("信息软删除") @Api ...
- javascript的HelloWorld
javascript:边解释边执行.是一种解释型语言. defer属性,延迟执行代码,(整个页面加载完之后,window.onload) 代码: <!DOCTYPE HTML PUBLIC &q ...
- TestNG学习地址分享
谨以此随笔来记录一个曾经学习TestNG的网站,供以后作为工具书查阅. 链接地址,点击此处.
- ele
vue饿了么app项目实战视频 5-1 1.项目代码规范修改.