Django框架中表单的用法
作者:springsnow 发布时间:2022-03-29 02:00:14
HTML表单是网站交互性的经典方式。 本章将介绍如何用Django对用户提交的表单数据进行处理。
一、HTTP 请求
HTTP协议以"请求-回复"的方式工作。客户发送请求时,可以在请求中附加数据。服务器通过解析请求,就可以获得客户传来的数据,并根据URL来提供特定的服务。
1、GET 方法
我们在之前的项目中创建一个 search.py 文件,用于接收用户的请求:
/HelloWorld/HelloWorld/search.py 文件代码:
# -*- coding: utf-8 -*-
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 and request.GET['q']:
message = '你搜索的内容为: ' + request.GET['q']
else:
message = '你提交了空表单'
return HttpResponse(message)
在模板目录 templates 中添加 search_form.html 表单:
/HelloWorld/templates/search_form.html 文件代码:
<!DOCTYPE html>
<html>
<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 规则修改为如下形式:
/HelloWorld/HelloWorld/urls.py 文件代码:
from django.conf.urls import url
from . import view,testdb,search
urlpatterns = [
url(r'^hello$', view.hello),
url(r'^testdb$', testdb.testdb),
url(r'^search-form$', search.search_form),
url(r'^search$', search.search),
]
访问地址 http://127.0.0.1:8000/search-form 并搜索,结果如下所示:
2、POST 方法
上面我们使用了GET方法。视图显示和请求处理分成两个函数处理。
提交数据时更常用POST方法。我们下面使用该方法,并用一个URL和处理函数,同时显示视图和处理请求。
我们在 templates 创建 post.html:
/HelloWorld/templates/post.html 文件代码:
<!DOCTYPE html>
<html>
<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 请求:
/HelloWorld/HelloWorld/search2.py 文件代码:
# -*- coding: utf-8 -*-
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 规则修改为如下形式:
/HelloWorld/HelloWorld/urls.py 文件代码:
from django.conf.urls import url
from . import view,testdb,search,search2
urlpatterns = [
url(r'^hello$', view.hello),
url(r'^testdb$', testdb.testdb),
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-post 显示结果如下:
完成以上实例后,我们的目录结构为:
HelloWorld
|-- HelloWorld
| |-- __init__.py
| |-- __init__.pyc
| |-- search.py
| |-- search.pyc
| |-- search2.py
| |-- search2.pyc
| |-- settings.py
| |-- settings.pyc
| |-- testdb.py
| |-- testdb.pyc
| |-- urls.py
| |-- urls.pyc
| |-- view.py
| |-- view.pyc
| |-- wsgi.py
| `-- wsgi.pyc
|-- TestModel
| |-- __init__.py
| |-- __init__.pyc
| |-- admin.py
| |-- admin.pyc
| |-- apps.py
| |-- migrations
| | |-- 0001_initial.py
| | |-- 0001_initial.pyc
| | |-- __init__.py
| | `-- __init__.pyc
| |-- models.py
| |-- models.pyc
| |-- tests.py
| `-- views.py
|-- db.sqlite3
|-- manage.py
`-- templates
|-- base.html
|-- hello.html
|-- post.html
`-- search_form.html
二、Request 对象
每个 view 函数的第一个参数是一个 HttpRequest 对象,就像下面这个 hello() 函数:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
HttpRequest对象包含当前请求URL的一些信息:
Request对象也有一些有用的方法:
QueryDict对象
在HttpRequest对象中, GET和POST属性是django.http.QueryDict类的实例。
QueryDict类似字典的自定义类,用来处理单键对应多值的情况。
QueryDict实现所有标准的词典方法。还包括一些特有的方法:
此外, QueryDict也有一些方法,如下表:
来源:https://www.cnblogs.com/springsnow/p/13170138.html


猜你喜欢
- 废话不多说 上语句:查询锁表语句:select object_name,machine,s.sid,s.serial#from v$lock
- 花了几个小时给小表弟普及了一下OOP的知识,索性总结一下写篇文章。OOP全称Object Oriented Programming即面向对象
- import React, { Component } from 'react';import { Table, Input
- 由于存在函数内部不能访问全局作用的,所以就需要一种可以引入上一级作用域的语法结构,可以通过use使用函数声明时所在作用域的变量的值。php的
- 在程序设计过程中,经常需要对输入的数据格式进行检查,这时就会用到正则表达式,匹配正则表达式则数据格式正确,否则格式错误。为了检查输入的数据是
- 零、前言python代码中配置文件是必不可少的内容。常见的配置文件格式有很多中:ini、yaml、xml、properties、txt、py
- 视图是一种常用的数据库对象,它将查询的结果以虚拟表的形式存储在数据中。因为视图有非常多的优点:1,可以简化操作,2,可以建立前台和后台的缓冲
- 根据"客服果果"的"十几行的超简日历组件"http://bbs.51js.com/viewthrea
- 什么是WebAPIWebAPI就是 DOM API + BOM APIDOMW3C标准给我们提供了一系列的函数,让我们可以操作:网页内容、网
- 提高SQL执行效率的几点建议:◆尽量不要在where中包含子查询;关于时间的查询,尽量不要写成:where to_char(dif_date
- 引言之前有段时间用postgresql 数据库,在上云之后,从自增主键变为uuid,感觉uuid全球唯一,很方便。最近用mysql,发现my
- 目录一. pymysql介绍二. 连接数据库的完整流程1. 引入pymysql模块2. 创建连接对象3. 使用连接对象创建游标对象4. 准备
- 1.开发环境 vue+element2.电脑系统 windows 10 专业版3.在开发的过程中,我们总是会使用到 git管理代码!使用方法
- 新搞了台linux云主机,瞎折腾折腾,先装个Python3。Linux环境下有其他软件需要Python2,如YUM,所以安装的Python3
- 一、基本使用①从属于time这个包②一般使用都是使用time.Time 这个类型表示时间 ,time包中还有一些常量,源码如下// Comm
- 目录pipenv 工作流1 .安装2.创建虚拟环境3.管理依赖4.pycharm设置虚拟环境总结pipenv 工作流Pipenv是基于pip
- 在开始之前还是提一下三个函数吧:"ob_start()、ob_end_clean()、ob_get_contents()"
- 一、引言Pillow,是Python Imaging Library (PIL)的一个分支,用于处理图像。它是Python编程语言中最常用的
- Any docsAny 是一种特殊的类型。静态类型检查器将所有类型视为与 Any 兼容,反之亦然, Any 也与所有类型相兼容。这意味着可对
- 摘 要:本文讨论了Visual Basic应用程序访问SQL Server数据库的几种常用的方法,分别说明了每种方法的内部机理并给出了每种方