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

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


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 renderdef 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});


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)