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)
- [Django]-Django content-type : how do I get an object?
- [Django]-How to test "render to template" functions in django? (TDD)
- [Django]-"<Message: title>" needs to have a value for field "id" before this many-to-many relationship can be used.
Source:stackexchange.com