0

Does anyone know why the following renders my template ok:

c= {'render_form' : form }
return render(request, 'page1.html', c)

but the following does not render the csrf token:

c= Context({'render_form' : form}) 
return render(request, 'page1.html', c)

The template looks like this:

<form method="post">
    {% csrf_token %}
    {{ render_form }}
     <input type="submit" value="Submit" class='btn' id="submitbutton" name="_submit" />
</form>

I want to keep render() and I would like to avoid using locals().

woodduck
  • 339
  • 2
  • 12
  • I don't understand why you would use Context explicitly though. Just pass a dictionary, that's what you're supposed to do. – Daniel Roseman Aug 14 '15 at 07:27
  • I thought I was supposed to. I was trying to follow the examples in https://docs.djangoproject.com/en/1.8/ref/templates/api/#playing-with-context. I'll just stick to the one that works then. – woodduck Aug 14 '15 at 12:05

1 Answers1

1

Depends on your Django version, but the render method used to take two context related arguments, context and context_instance, the latter expects a Context or RequestContext object, the first a dictionary. The documentation has some specific deprecation details:

https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#optional-arguments

Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100