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, 真是醉了!记一下,免得以后再犯错了.
随机推荐
- sele nium 模块
python3 web测试模块selenium 阅读目录 1.selenium安装配置 2.Selenium的基本使用 (1)声明浏览器对象 (2)定位元素 (3)元素对象(element) (4 ...
- 总结day12 ----装饰器
一,什么是装饰器? 装饰器本质上就是一个python函数,他可以让其他函数在不需要做任何代码变动的前提下,增加额外的功能,装饰器的返回值也是一个函数对象. 装饰器的应用场景:比如插入日志,性能测试,事 ...
- 常用DOS命令和Linux命令
DOS命令 1.查询端口占用情况:netstat -aon |findstr "8080"; 查看端口进程号: 2.查看进程号信息: tasklist |findstr &qu ...
- 04-树5 Root of AVL Tree (25 分)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- wireshark 抓包
Wireshark(前称Ethereal)是一个网络数据包分析软件.网络数据包分析软件的功能是截取网络数据包,并尽可能显示出最为详细的网络数据包数据.Wireshark使用WinPCAP作为接口,直接 ...
- c# SocketAsyncEventArgs类的使用 IOCP服务器
要编写高性能的Socket服务器,为每个接收的Socket分配独立的处理线程的做法是不可取的,当连接数量很庞大时,服务器根本无法应付.要响应庞大的连接数量,需要使用IOCP(完成端口)来撤换并处理响应 ...
- vue中的双向数据绑定详解
前言 什么是数据双向绑定? vue是一个mvvm框架,即数据双向绑定,即当数据发生变化的时候,视图也就发生变化,当视图发生变化的时候,数据也会跟着同步变化.这也算是vue的精髓之处了.值得注意的是,我 ...
- 【Eclipse】在Project Explore中隐藏不需要显示的文件
右击项目->Resource->Resource Filters->AddFilter type: Exclude allApplies to : Files (All childr ...
- php工具箱使用
linux安装过程 1,去网盘下载(本人网盘也有,如果找不到,contact me)http://pan.baidu.com/s/1cxHQge 我解压以后放在/mnt/hgfs/root/phpto ...
- JS实现瀑布流
HTML:先让图片充满一页 <!DOCTYPE html> <html> <head lang="en"> <meta charset=& ...