CSRF-verification-failed-Request-aborted
About the CSRF_Token (From Django Official Document) The CSRF middleware and template tag provides easy-to-use protection against Cross Site Request Forgeries. This type of attack occurs when a malici
目录4 个章节
About the CSRF_Token
(From Django Official Document)
The CSRF middleware and template tag provides easy-to-use protection against Cross Site Request Forgeries. This type of attack occurs when a malicious website contains a link, a form button or some JavaScript that is intended to perform some action on your website, using the credentials of a logged-in user who visits the malicious site in their browser. A related type of attack, ‘login CSRF’, where an attacking site tricks a user’s browser into logging into a site with someone else’s credentials, is also covered.
The first defense against CSRF attacks is to ensure that GET requests (and other ‘safe’ methods, as defined by RFC 7231#section-4.2.1) are side effect free. Requests via ‘unsafe’ methods, such as POST, PUT, and DELETE, can then be protected by following the steps below.
The error
According to the official documentation, I only needed to add csrf_token to forms that submit via POST. Even so, I still ran into this problem. I went through the docs a few more times, found nothing wrong, then searched on Google and discovered it was a template-rendering issue.
The book I was reading recently, 16 Lessons of Django, really led me astray. Following its approach caused all sorts of problems—what a trap.
Solution
The original approach was to first get a template object with the get_template function, then render it into HTML. That’s exactly where the problem was: HTML rendered this way does not include the csrf_token!
Using django.shortcuts.render as described in the official documentation does not have this problem.
Official code, while I’m at it
views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .forms import NameForm
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
name.html
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
Also attaching the official explanation of Cross Site Request Forgery protection Forms and Cross Site Request Forgery protection
Django ships with an easy-to-use protection against Cross Site Request Forgeries. When submitting a form via
POSTwith CSRF protection enabled you must use thecsrf_tokentemplate tag as in the preceding example. However, since CSRF protection is not directly tied to forms in templates, this tag is omitted from the following examples in this document.
_EOF_
CSRF-verification-failed-Request-aborted
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Django与Vue.js模板语法冲突问题的解决
问题 django模板与vue.js的变量都是使用 {{ 和 }} 包裹起来的,在渲染django模板时会先替代掉所有的 {{ 和 }}及被包裹在其中的内容,使得vue.js没办法用 {{ 和 }}来绑定变量。 解决方法 方法1:修改vue.js的默认的绑定符号 在Vue App创建时配置 let app = new Vue({ delimiters: [[[, ]]], el: '#vu
2022-08-26 · 0_Django开发常见问题 · 约 1 分钟
解决Django使用Jinja2模版引擎之后admin无法访问的问题
需要两个文件 说明:这两个文件放在哪里都是ok,但是要记住路径,待会配置的时候需要输入。 建议放在和settings.py同个目录下面。 创建backends.py文件: import sys from django.template.backends import jinja2 as jinja2backend from django.template.backends.utils impor
2022-05-27 · 0_Django开发常见问题 · 约 1 分钟
编码规范和版本管理
https://www.zhihu.com/question/393040445 合并发布, 可以参考Git Flow. 但是, 如果你做Trunk-based Development的话, 这个也可以省略了. 作者:Jacky 链接:https://www.zhihu.com/question/393040445/answer/1215427425 来源:知乎 著作权归作者所有。商业转载请联系作
2021-12-18 · 团队 · 约 1 分钟
Asp-Net-Core开发笔记:API版本管理
对于Web API应用程序而言,随着时间的推移以及需求的增加或改变,API必然会遇到升级的需求。事实上,Web API应用程序应该从创建时就考虑到API版本的问题。业务的调整、功能的增加、接口的移除与改名、接口参数变动、实体属性的添加、删除和更改等都会改变API的功能,从而带来版本的变更。
2023-06-09 · Asp-Net-Core开发笔记 · 约 1 分钟
讨论
共 0 条加载评论中…