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, 真是醉了!记一下,免得以后再犯错了.
随机推荐
- DataList用法总结
设计模版: 页眉<HeaderTemplate> </HeaderTemplate> 页脚<FooterTemplate> </FooterTemplat ...
- Eclipse署动态web项目方法
和MyEclipse不一样,在Eclipse中做的Web项目默认是不支持将项目发布到Web服务器上的,会发布到工作空间的某个目录,因此无法在外部启动Tomcat来运行Web项目,只有打开Eclipse ...
- Maven 依赖管理问题小计
刚学Maven,遇到点小问题,记录一下.https://maven.apache.org/ 问题的起因是项目中使用了 Hibernate Validator ,但是运行起来后总是不能按照设置的注解校验 ...
- Machine learning 第7周编程作业 SVM
1.Gaussian Kernel function sim = gaussianKernel(x1, x2, sigma) %RBFKERNEL returns a radial basis fun ...
- Django环境搭建之hello world
当我们想用Python来开发一个web应用时,首先要选择一个优秀的web框架,Django是个非常成熟的web开发框架,网上具有丰富的文档和学习资料,所以选择Django框架来入门web开发是个不错的 ...
- Sweep Line
391. Number of Airplanes in the Sky https://www.lintcode.com/problem/number-of-airplanes-in-the-sky/ ...
- Mac下运行git报错"xcrun: error: invalid active developer path .."
错误:xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun ...
- 再学C/C++ 之 指针常量 和 常量指针
(1)指针常量,将指针的指向当成常量.即指针变量的值只能在定义的时候初始化,定义后不能修改,也就是说不能改变指针变量的指向.但是不影响所指对象的访问特征.其定义形式为: 类型 * const 指针 / ...
- java反射机制_读取properties
代码: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * 反射特点: ...
- accessToken的使用
1.accessToken是啥,干嘛用? 形象解释:申请调兵-->皇帝同意-->兵符-->开始调兵 拿到用户在第三方平台的唯一的标识; 获取用户的nickname,头像,邮箱等其他信 ...