django中的csrf与后台模块处理
来源:互联网 发布:淘宝如何绑定银行卡 编辑:程序博客网 时间:2024/06/01 19:19
在表单中使用post方法提交表单数据,需要使用csrf标签,这是Django提供的防止伪装提交请求的功能。get方法提交数据不用使用csrf
用法:
模板中:
<form action="/query" method="post">
{% csrf_token %}
<input type="text" name="productname">
<input type="submit" value="查询">
</form>
模块中:
def query(request):
if request.POST.has_key("productname"):
products = Product.objects.filter(name = request.POST["productname"])
else:
products = Product.objects.all()
pagenum = 1
if request.GET.has_key("pid"):
pagenum = request.GET['pid']
p = Paginator(products,2)
page = p.page(pagenum)
return render_to_response("index.html",{"user":request.user,"p":p,"page":page},context_instance = RequestContext(request))
然而,在模块处理环节,使用render_to_response返回数据时总是报错csrf验证失败根据网上查找原因如下:
网上原文:
The context_instance
parameter in render_to_response
wasdeprecated in Django 1.8, and removed in Django 1.10.
The solution is to switch to the render
shortcut, which automatically uses a RequestContext
.
Update your imports and view as follows. Note that render
takes therequest
object as its first argument.
from django.shortcuts import renderdef my_view(request): context = {'foo': 'bar'} return render(request, 'my_template.html', context)
在1.10版本的django中已经移除了context_instance参数,而我使用的是1.11版本
方法是换用render方法返回数据:
return render(request,"index.html",{"user":request.user,"p":p,"page":page})
简单快捷。
- django中的csrf与后台模块处理
- Django CSRF处理
- django中的csrf
- django中的CSRF
- django 处理POST请求时报403 -- csrf
- django 对于csrf错误的处理
- Django, csrf
- django CSRF
- 模拟登录Django csrf验证 及 django_cas_server 模块验证
- Django中的视图模块
- Django-- admin后台管理模块的使用
- [django]总结Django中的用户权限模块
- [django]总结Django中的用户权限模块
- 总结Django中的用户权限模块
- 总结Django中的用户权限模块
- 总结Django中的用户权限模块
- Django Ajax with CSRF
- django csrf 403
- Https、SSL/TLS以及证书入门
- 解读 Linux 性能指标
- 【积累】 int 10进制 byte 16进制,BCD码 String之间的转换
- Struts2开发基础
- 半兼容ARM的软核处理器编写-5
- django中的csrf与后台模块处理
- 响铃:秒拍掘金垂直化内容,到底是不是个好主意?
- 原型
- Http协议简介
- js_Array
- 【Shawn-LanQiao】第八届蓝桥杯国赛JavaB组
- for ture love
- MySQL--修改数据表5:删除主键约束,唯一约束,外键约束
- 反射工具类(调用父类的方法和字段)