Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12095 - login and other contrib views failing if template rend…

…ered using inclusion tag.

The {% csrf_token %} tag is unable to get its value if a template is
rendered using an inclusion_tag, since that creates a brand new Context,
rather than using the existing one.  Since this is a common pattern, and we
need CSRF protection to be as simple and easy as possible, we special case
the csrf_token and copy it from the parent context to the new context.

A more elegant and general solution may appear in future, but this is good
enough for now.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@11672 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 43c2ed0eb3f9996539f03e4ad68a08534023659a 1 parent 4281bf3
Luke Plant authored October 27, 2009

Showing 1 changed file with 8 additions and 2 deletions. Show diff stats Hide diff stats

  1. 10  django/template/__init__.py
10  django/template/__init__.py
@@ -942,8 +942,14 @@ def render(self, context):
942 942
                         else:
943 943
                             t = get_template(file_name)
944 944
                         self.nodelist = t.nodelist
945  
-                    return self.nodelist.render(context_class(dict,
946  
-                            autoescape=context.autoescape))
  945
+                    new_context = context_class(dict, autoescape=context.autoescape)
  946
+                    # Copy across the CSRF token, if present, because inclusion
  947
+                    # tags are often used for forms, and we need instructions
  948
+                    # for using CSRF protection to be as simple as possible.
  949
+                    csrf_token = context.get('csrf_token', None)
  950
+                    if csrf_token is not None:
  951
+                        new_context['csrf_token'] = csrf_token
  952
+                    return self.nodelist.render(new_context)
947 953
 
948 954
             compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode)
949 955
             compile_func.__doc__ = func.__doc__

0 notes on commit 43c2ed0

Please sign in to comment.
Something went wrong with that request. Please try again.