Django 1.9 CSRF verification failed. Request aborted

后端 未结 1 2009
情书的邮戳
情书的邮戳 2021-01-25 22:06

I have a problem with CSRF. I know must use: {% csrf_token %} but the problem is the same.

This my form:

{
相关标签:
1条回答
  • For the {% csrf_token %} tag to work, you must ensure that the template is rendered with the request object (see the docs).

    The easiest way to do this is to use the render shortcut instead of render_to_response. The render_to_response method is not recommended, and is likely to be removed from Django in future.

    from django.shortcuts import render
    
    def registro(request):
        c = {}  
        # put any other context you need in c
        # no need for c.update(csrf(request)) because we're using render    
        return render(request, 'registro.html', c)
    

    You need to update any views that use csrf_token in their templates. In this case, you haven't updated the home view that renders the form in the index.html template.

    def home(request):
        articulos = Articulo.objects.all().order_by('-fecha')
        return render(request, 'index.html', {'articulos':articulos})
    
    0 讨论(0)
提交回复
热议问题