简体   繁体   English

使CSRF中间件在Django 404错误页面中工作

[英]Make CSRF middleware work in Django's 404 error pages

I put a login box alone with a keyword search box in 404.html in a Django project so in case a 404 error is raised, visitors get more options to jump to other parts. 我在Django项目中的404.html中单独放置了一个带有关键字搜索框的登录框,以防万一出现404错误,访问者有更多选择可以跳到其他部分。

But the CSRF middleware doesn't work in 404 error page with no csrf token rendered. 但是CSRF中间件不能在没有csrf令牌呈现的404错误页面中工作。 I tried move 'django.middleware.csrf.CsrfViewMiddleware' to first of MIDDLEWARE_CLASSES in settings.py but did not work either. 我尝试将'django.middleware.csrf.CsrfViewMiddleware'移到settings.py中的MIDDLEWARE_CLASSES的第一个,但也没有用。

Anyone knows a solution? 有人知道解决方案吗?

Looks like they've fixed this in Django http://code.djangoproject.com/ticket/14565 看起来他们已经在Django http://code.djangoproject.com/ticket/14565中修复了此问题

That said, it doesn't seem to be in the latest release, so I created a custom error view with the same contents as 'django.views.defaults.page_not_found' , making the changes found in the ticket diff file. 也就是说,它似乎不是最新版本,因此我创建了一个自定义错误视图,其内容与'django.views.defaults.page_not_found'相同,并在票证差异文件中进行了更改。

我的想法是,您应该创建自己的错误处理程序,并使用django.middleware.csrf.get_token()手动生成令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM