1.HTTP请求

HTTP协议以"请求-回复"的方式工作。

客户发送请求时,可以在请求中附加数据。服务器通过解析请求,就可以获得客户传来的数据,并根据URL来提供特定的服务。

(1)GET方法

在项目中HelloWorld/HelloWorld下创建一个 search.py 文件,用于接收用户的请求

 from django.http import HttpResponse
from django.shortcuts import render_to_response # 表单
def search_form(request):
return render_to_response('search_form.html') # 接收请求数据
def search(request):
request.encoding='utf-8'
if 'q' in request.GET:
message = '你搜索的内容为:' + request.GET['q']
else:
message = '你提交了空表单'
return HttpResponse(message)

在模板目录 templates 中添加 search_form.html 表单

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>runoob.com</title>
</head>
<body>
<form action="/search" method="get">
<input type="text" name="q">
<input type="submit" value="搜索">
</form>
</body>
</html>

urls.py 规则修改为如下形式

 from django.conf.urls import url
from . import view, search, search2 # 匹配模式 统一资源定位
urlpatterns = [
# url(r'^$', view.hello)
url(r'^hello$', view.hello),
url(r'^search-form$', search.search_form),
url(r'^search$', search.search),
url(r'^search-post$', search2.search_post)
]
# 正则表达式 ^$表示匹配一行的首和尾

访问地址 http://127.0.0.1:8000/search-form 或是http://localhost:8000/search-form并搜索

注:传不上图片啊

(2)POST方法

上面使用了GET方法。视图显示和请求处理分成两个函数处理。

提交数据时更常用POST方法。下面使用该方法,并用一个URL和处理函数,同时显示视图和处理请求。

在tmplate 创建 post.html

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>runoob.com</title>
</head>
<body>
<form action="/search-post" method="post">
{% csrf_token %}
<input type="text" name="q">
<input type="submit" value="Submit">
</form> <p>{{ rlt }}</p>
</body>
</html>

在模板的末尾,增加一个 rlt 记号,为表格处理结果预留位置。

表格后面还有一个{% csrf_token %}的标签。

csrf 全称是 Cross Site Request Forgery。

这是Django提供的防止伪装提交请求的功能。POST 方法提交的表格,必须有此标签。

在HelloWorld目录下新建 search2.py 文件并使用 search_post 函数来处理 POST 请求

 from django.shortcuts import render
from django.views.decorators import csrf # 接收POST请求数据
def search_post(request):
ctx = {}
if request.POST:
ctx['rlt'] = request.POST['q']
return render(request, "post.html", ctx)

urls.py 规则修改为如下形式

 from django.conf.urls import url
from . import view, search, search2 # 匹配模式 统一资源定位
urlpatterns = [
# url(r'^$', view.hello)
url(r'^hello$', view.hello),
url(r'^search-form$', search.search_form),
url(r'^search$', search.search),
url(r'^search-post$', search2.search_post)
]
# 正则表达式 ^$表示匹配一行的首和尾

访问 http://localhost:8000/search-post 显示结果

注:此次图片无法上传

2. Requset对象

更多与HttpRequest对象的信息参看 http://www.runoob.com/django/django-form.html

Django学习笔记2:处理表单的更多相关文章

  1. Ext JS4 学习笔记之发送表单(Form)时也将表单下的表格(Grid)数据一同发送的方法

    Ext JS4 学习笔记之发送表单(Form)时也将表单下的表格(Grid)数据一同发送的方法 昨天在开发的时候遇到个小问题,就是如何将Grid的内容与Form一起发送到服务器端.默认情况下,表单(F ...

  2. Django学习笔记(7)——单表操作和多表操作

    单表操作 1,创建模型 创建名为book的APP,在book下的models.py中创建模型: from django.db import models # Create your models he ...

  3. Django学习(4)表单,让数据库更美好

    表单,在HTML中的标签为<form></form>,在网页中主要负责数据采集功能.我们在浏览网站时,常常会碰到注册账号.账号登录等,这就是表单的典型应用. 在Django学习 ...

  4. HTML5学习笔记<五>: HTML表单和PHP环境搭建

    HTML表单 1. 表单用于不同类型的用户输入 2. 常用的表单标签: 标签 说明 <form> 表单 <input> 输入域 <textarea> 文本域 < ...

  5. Django学习系列之Form表单结合ajax

      Forms结合ajax Forms的验证流程: 定义用户输入规则的类,字段的值必须等于html中name属性的值(pwd= forms.CharField(required=True)=<i ...

  6. knockoutJS学习笔记08:表单域绑定

    前面的绑定都是用在基本标签上,这章主要讲表单域标签的绑定. 一.value 绑定 绑定标签:input text.textarea. <p>用户名:<input type=" ...

  7. HTML 学习笔记 JQuery(表单,表格 操作)

    表单应用 一个表单有3个基本组成部分 (1)表单标签:包含处理表单数据所用的服务器端程序URL 以及数据提交到服务器的方法 (2)表单域:包含文本框 密码框 隐藏框 多行文本框 复选框 单选框 下拉选 ...

  8. redux-form的学习笔记二--实现表单的同步验证

    (注:这篇博客参考自redux-form的官方英文文档)左转http://redux-form.com/6.5.0/examples/syncValidation/ 在这篇博客里,我将用redux-f ...

  9. HTML学习笔记8:表单

      什么是表单?     一个网页表单可以将用户输入的数据发送到服务器进行处理.因为互联网用户使用复选框,单选按钮或文本字段填写表格,所以WebForms的形式类似文件或数据库.例如,WebForms ...

  10. PHP学习笔记 01 之表单传值

    一.HTML传值/PHP接收方法 1.GET(地址栏+问号+数据信息) (1)方式一:表单Form: method = 'get' GET接收数据方式: $_GET['表单元素name对应的值] (2 ...

随机推荐

  1. diskimage-builder-command

    yum -y install python-virtualenv.noarch virtualenv ~/dib-virtualenv . ~/dib-virtualenv/bin/activate ...

  2. 附录:python and numpy

    http://cs231n.github.io/python-numpy-tutorial/ Python is a great general-purpose programming languag ...

  3. 爬虫:Scrapy13 - 发送 email

    虽然 Python 通过 smtplib 库使得发送 email 变得非常简单,Scrapy 仍然提供了自己的实现.该功能十分易用,同时由于采用了 Twisted 非阻塞式(non-blocking) ...

  4. SPOJ AMR10I Dividing Stones

    Time limit: 7s Source limit: 50000B Memory limit: 256MB The first line contains the number of test c ...

  5. 网络namespace

    sduo sysctl -w net.ipv4.conf.all.forwarding=1 sudo iptables -t nat -A  POSTROUTING -s 172.18.0.0/24 ...

  6. 【HDU 2594 Simpsons' Hidden Talents】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  7. 如何设置两个AD域控制器

    目的:这要为了测试,哈哈哈哈. 其实是为了AD域控的高可用性.一个域控down了,另一个可以顶上去. 如何设置:参考http://lgzeng2360.blog.51cto.com/275998/10 ...

  8. java中的UDP总结

    先说一下关于InetAddress类,用一个小例子: import java.net.InetAddress; import java.net.UnknownHostException; public ...

  9. 关闭vscode打开新文件自动关闭预览文件功能

    经常碰到这个问题,我打开文件就是有用的,每次给我自动关闭了我还得去打开. 当然这个问题可以双击文件,接触那个文件的预览状态就可以解决了.不过还有一个更懒的方法,直接修改vscode配置就好了. // ...

  10. Quartus2 通过Nativelink调用modelsim进行功能仿真(转载)

    quartus2建立工程后,编译并检查语法通过后(功能仿真都不需要综合) tips:这样你的工程层次化也同时完成了. 打开Assignment -> settings, 找到Simulation ...