简体   繁体   中英

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.

But the CSRF middleware doesn't work in 404 error page with no csrf token rendered. I tried move 'django.middleware.csrf.CsrfViewMiddleware' to first of MIDDLEWARE_CLASSES in settings.py but did not work either.

Anyone knows a solution?

Looks like they've fixed this in 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.middleware.csrf.get_token()手动生成令牌。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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