[Django]-Django – {% csrf_token %} was used in a template, but the context did not provide the value

34👍

Your index.html is rendered without RequestContext. Try this:

def index(request):
    return render_to_response('index.html', context_instance=RequestContext(request))

I also recomend you to use more convenient shortcut render:

from django.shortcuts import render

def index(request):
    return render('index.html')

From docs:

render() is the same as a call to render_to_response() with a
context_instance argument that forces the use of a RequestContext.

EDIT:

Thanks @nerdwaller for mentioning, newer versions now needs:

render(request, 'index.html', {params});

4👍

I was getting this error using Django 2.1, turned out that it was caused by make an ajax request in the template that was called by another ajax request. So, the solution was to add ‘request=request’ to my render function:

args = {'someargs': somevalues}
html = render_to_string(
        'my_template.html', context=args, request=request)
return HttpResponse(html)

Leave a comment