Ticket #15855: 15855_CSRF_per_view_caching_docs.diff

File 15855_CSRF_per_view_caching_docs.diff, 1.8 KB (added by Idan Gazit, 13 years ago)
  • docs/ref/contrib/csrf.txt

    diff --git a/docs/ref/contrib/csrf.txt b/docs/ref/contrib/csrf.txt
    index 8dc1373..80aefb1 100644
    a b methods are assumed to be unsafe, for maximum protection.  
    227227Caching
    228228=======
    229229
     230.. admonition:: Caching and CSRF don't mix well
     231
     232    Mixing CSRF protection and caching is inherently tricky. You have to make
     233    sure that you aren't caching the CSRF token, which can lead to subtle
     234    bugs.
     235
    230236If the :ttag:`csrf_token` template tag is used by a template (or the
    231237``get_token`` function is called some other way), ``CsrfViewMiddleware`` will
    232238add a cookie and a ``Vary: Cookie`` header to the response. This means that the
    233239middleware will play well with the cache middleware if it is used as instructed
    234240(``UpdateCacheMiddleware`` goes before all other middleware).
    235241
    236 However, if you use cache decorators on individual views, the CSRF middleware
    237 will not yet have been able to set the Vary header.  In this case, on any views
    238 that will require a CSRF token to be inserted you should use the
    239 :func:`django.views.decorators.vary.vary_on_cookie` decorator first::
    240 
    241   from django.views.decorators.cache import cache_page
    242   from django.views.decorators.vary import vary_on_cookie
    243 
    244   @cache_page(60 * 15)
    245   @vary_on_cookie
    246   def my_view(request):
    247       # ...
     242:ref:`Per-view caching <cache-per-view>` is not compatible with the :ttag:`csrf_token` template tag, as
     243the entire view (including the CSRF token in your template) will be cached and
     244sent upon subsequent request.
    248245
    249246
    250247Testing
  • docs/topics/cache.txt

    diff --git a/docs/topics/cache.txt b/docs/topics/cache.txt
    index 0dbe844..2281a3e 100644
    a b language.  
    491491
    492492__ `Controlling cache: Using other headers`_
    493493
     494.. _cache-per-view:
     495
    494496The per-view cache
    495497==================
    496498
Back to Top