实现方法:

1,可以先定义一个基础的页面访问路径 例如:http://127.0.0.1:8000/index/  定义index路径

在urls

 urlpatterns = [

     url(r'^index/$', views.index),

 ]

2,同时也需要创造一个index.html页面

<html xmlns="http://www.w3.org/1999/html">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/> <title>登陆页面</title>
</head>
<body>
<form method="post" action="/login_action/"> <!--创造一个表单,用于数据提交,并且使用post方式提交表单,同时定义为login_action(登陆)过程-->
<input name="username" type="text" placeholder="用户名"><br>         <!--input标签定义文本框,数据类型-->
<input name="password" type="password" placeholder="密码"><br>
{{error}}<br>                                    <!--这里的双大括号可以用于显示函数所指定的内容-->
<button id="btn" type="submit">登陆</button>
{% csrf_token %}         <!--为了防止csrf攻击--> </form>
</body>
</html>

3,需要一个将url和html连接起来的函数

定义views.py

 from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect #这三个模块为常用模块
# Create your views here. def index(request):
return render(request, 'index.html')
def login_action(request):
if request.method == 'POST': #判断是否为post提交方式
username = request.POST.get('username', '') #通过post.get()方法获取输入的用户名及密码
password =request.POST.get('password', '') if username == 'admin' and password == '':          #判断用户名及密码是否正确
return HttpResponseRedirect('/event_manage/')      #如果正确,(这里调用另一个函数,实现登陆成功页面独立,使用HttpResponseRedirect()方法实现
else:
return render(request,'index.html',{'error':'username or password eror'})#不正确,通过render(request,"index.html")方法在error标签处显示错误提示 def event_manage(request): #该函数定义的是成功页面的提示页面 #username =request.COOKIES.get('user', '') #读取浏览器cookie
return render(request,"event_manage.html") #{"user":username})         #在上面的函数判断用户名密码正确后在显示该页面,指定到event_manage.html,切换到一个新的html页面

使用到的方法包括

render()

POST.get()

HttpResponseRedirect()
HttpResponse()

熟悉它们的使用

  login_action()函数,对应index.html中定义的表单的提交过程,我们在这个过程中提交数据并且判断数据,

event_manage()函数用于打开新的html页面

5,创造成功页面event_manage.HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>成功</title>
</head>
<body>
<h1>你好,入侵者</h1> </body>
</html>

在views.py 中定义的函数都应该在urls.py 中定义路径路径名称可以自定,但要与函数名称对应,这里为了与相应的功能对应

6,定义上面的两个函数的路径

urlpatterns = [

    url(r'^index/$', views.index),
url(r'^login_action/$', views.login_action), #登陆过程
url(r'^event_manage/$', views.event_manage),   #成功的页面 ]

总结一下这整个流程

首先,我们通过http://127.0.0.1:8000/index/访问基础登陆页面

输入用户名密码,点击提交按钮,这一过程(login_action)调用login_action()函数{并且跳转到http://127.0.0.1:8000/login_action/}——————进行判断----------正确---------立马跳转到http://127.0.0.1:8000/event_manage/  ,并且显示event_manage.html.

整个实现过程

首先创造一个路径,相应的html页面

然后通过一个函数将他们捆绑到一起

实现表单内容提交的过程再定义一个函数用于处理数据,又定义一个函数,用于指定跳转到其它 的页面

总之,在views.py 中定义的是处理html中各种数据处理,数据判断,页面的跳转

同时定义的这些函数都是各个过程方法的链接,也应该在urls.py中创造这些路径

最新文章

  1. testng 教程
  2. 使用Microsoft Fakes进行单元测试(1)
  3. html5,导航
  4. 每日Scrum--No.6
  5. JVM的GC理论详解
  6. css构造文本
  7. 高并发应用中客户端等待、响应时间的推算,及RT/QPS概念辨析
  8. JavaScript 经常忽略的 7 个基础知识点
  9. xml--小结②XML的基本语法
  10. [转] 函数编程之闭包漫谈(Closure)
  11. 从excel读数据到informix的Found a quote for which there is no matching quote错误
  12. 移动前端不得不了解的HTML5 head 头标签(首篇)
  13. web3 - BOM&amp;DOM
  14. dubbo在idea下的使用创建 服务者,消费者 注册中心
  15. C++内存泄漏检测工具
  16. day34 基于TCP和UDP的套接字方法 粘包问题 丢包问题
  17. LOJ#6433. 「PKUSC2018」最大前缀和 状压dp
  18. Web设计中打开新页面或页面跳转的方法
  19. 20165336 2017-2018-2《Java程序设计》第6周学习总结
  20. 实验long raw 和 blob两种数据类型遇到dblink的表现

热门文章

  1. [skill][gdb][coredump][abrt] 使用abrt管理程序coredump
  2. 内部排序-&gt;交换排序-&gt;快速排序
  3. ini文件读写
  4. webpack打包配置模板
  5. 占满屏幕的宽高,当把textarea换成其他标签的时候,怎么才能编辑?
  6. 转: js实现全角半角检测的方法
  7. Android支持全面屏设置
  8. 报错解决——make: *** No targets specified and no makefile found. Stop
  9. Java中的boxing和unboxing(转)
  10. Django进阶Model篇—数据库操作(ORM)