返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
0_Django开发常见问题

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

曦远 (DealiAxy)约 7 分钟
目录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.

错误

根据官方文档的说明,我在需要使用POST提交的表单里添加csrf_token即可,然而,还是出现了这个问题,又多看了几遍文档,发现没有错误,然后到谷歌一搜,才发现是模版渲染的问题。 最近看的这本《Django的16堂课》真是害人不浅啊,按照里面的写法问题多多,太坑了。

解决

原本的写法是先用get_template函数获取一个模版对象,然后在render渲染成html,问题就是出在这里,这种方式渲染的html,并不会把加入csrf_token! 使用官方文档里介绍的django.shortcuts.render就没有这个问题。

顺手贴上官方代码

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>

再附上官方对于伪造跨站访问请求保护的说明 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 POST with CSRF protection enabled you must use the csrf_token template 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 许可协议。转载请注明出处!

相关文章

我用Qt和Python开发了一个视频合并工具

想合并视频,但是却找不到比较好用的工具,很多都要收费,我想着「这破功能还得收费?」,于是决定自己搞一个,一开始用Python做了一个,效果还不错,完美完成了我的视频合并任务,不过没有图形界面,想分享给朋友一起用也没办法,于是昨天用Qt写了个图形界面套上去,中间还是遇到一些问题的,通过这篇文章记录一下。 软件的界面就是这样啦,用Qt Designer随便拖出来的,和VS拖控件也差不了多少。 用起来挺

2021-12-18 · Python · 约 1 分钟

解决drf-yasg中的SwaggerAPI无法正确分组问题

swagger是后台开发中很好用的交互式文档,Django原本的Django-Swagger已经停止维护了,现在一般用drf_yasg这个包来实现文档,它里面支持swagger和redoc两种,redoc是静态的,作为导出文档的话不错,不过一般我们用swagger,因为可以在文档里面调试,非常方便。 Drf里面有个东西是AutoSchema,可以自动扫描ViewSet和APIView这类可以提供接

2022-04-04 · 0_Swagger与drf-yasg接口文档系列 · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…