(Django) CSRF Verification for AJAX requests working in Chrome but not Firefox

给你一囗甜甜゛ 提交于 2019-12-05 23:58:01

In your request headers, I see:

X-CSRFToken null

So my guess is that the cookie is being set in Firefox. Perhaps it was already set in Chrome from a previous session.

The Django docs explain one reason why this may be:

Warning

If your view is not rendering a template containing the csrf_token template tag, Django might not set the CSRF token cookie. This is common in cases where forms are dynamically added to the page. To address this case, Django provides a view decorator which forces setting of the cookie: ensure_csrf_cookie().

Try importing the ensure_csrf_cookie decorator in your views.py and wrapping your base view with it. Ex:

from django.views.decorators.csrf import ensure_csrf_cookie

@ensure_csrf_cookie
def base_view(request):
    # do stuff
    return render('base.html', {...})

I'm not sure if this is the root issue, but I hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!