Django之--POST方法处理表单请求
上一篇:Django之--MVC的Model 演示了如何使用GET方法处理表单请求,本文讲述直接在当前页面返回结果,并使用更常用的POST方法处理。
Post方法与get方法的区别网上有很多这里不再详述,相对来说POST更加安全和用途多样,get多用于不包含敏感信息的查询。
一、首先我们修改下page.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!DOCTYPE html> <html> <h3>{{ 标题 }}</h3> <body> <p> {% for 商品 in 商品列表 %} <li><font face= "verdana" color= "blue" size =4>{{ 商品 }}</font></li> {% endfor %} </p> <br> <form action = "/product" method= "post" > #修改为/product,方法修改为post,我们通过此url展示商品和查询结果 {% csrf_token %} #添加1 <input type= "text" name = "q" > <input type= "submit" value= "查看商品信息" > </form> <p>{{ 结果 }}</p> #添加2:这里预留一个结果显示行 </body> </html> |
{% csrf_token %}的标签:csrf 全称是 Cross Site Request Forgery。这是Django提供的防止伪装提交请求的功能。POST 方法提交的表格,必须有此标签。
二、然后我们编写product.py文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | from django.shortcuts import render from django.views.decorators import csrf from . import mysql def page(request): context={} context[ '标题' ] = '商品种类:' pro_list=mysql.db_query( "select distinct name from product" ) context[ '商品列表' ]=[] for i in range(0,len(pro_list)): context[ '商品列表' ].append(pro_list[i][0]) if request.POST: pro=request.POST[ 'q' ] if not pro.strip(): context[ '结果' ] = '搜索项不能为空' else : price_quan=mysql.db_query( "select price,quantity from product where name='%s'" %(pro)) price=str(price_quan[0][0]) quantity=str(price_quan[0][1]) context[ '结果' ] = '你搜索的商品为: ' + pro + '商品价格为:' + price + '商品余量为:' + quantity return render(request, 'page.html' ,context) |
然后这里再贴一下上一篇GET方法的写法作对比:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # -*- coding: utf-8 -*- from django.http import HttpResponse from django.shortcuts import render # HttpResponse与render的区别在于,前者只用于返回文本信息,而render即其字面意思“渲染”,意思是使用context渲染html页面后返回。 from . import mysql # 表单 def page(request): context = {} context[ '标题' ] = '商品种类:' pro_list = mysql.db_query( "select distinct name from product" ) context[ '商品列表' ] = [] for i in range ( 0 , len (pro_list)): context[ '商品列表' ].append(pro_list[i][ 0 ]) return render(request, 'page.html' ,context) # 接收请求数据 def result(request): request.encoding = 'utf-8' pro = request.GET[ 'q' ] if not pro.strip(): message = '搜索项不能为空' else : price_quan = mysql.db_query( "select price,quantity from product where name='%s'" % (pro)) price = str (price_quan[ 0 ][ 0 ]) quantity = str (price_quan[ 0 ][ 1 ]) message = '你搜索的商品为: ' + pro + '商品价格为:' + price + '商品余量为:' + quantity return HttpResponse(message) |
三、最后修改下urls.py
1 2 3 4 5 6 7 | from django.conf.urls import url from . import view ,testdb,search,product urlpatterns = [ url(r '^hello$' , view .hello), url(r '^product$' , product.page), ] |
最后的展示结果如下:
建了一个数据库和编程的交流群,用于交流和提升能力,目前主要专注于Golang/Java/Python以及TiDB数据库,群号:231338927,建群日期:2019.04.26。
如发现博客错误,可直接留言指正,感谢。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)