Login

Snippets by smoonen

Snippet List

Caching XHTML render_to_response

This code improves on Django's render_to_response shortcut function in the following ways: 1. If the caller does not provide a MIME type, and the caller is passing a RequestContext, it interrogates the request to determine if the HTTP client supports application/xhtml+xml encoding. If so, it sets the request type to application/xhtml+xml to override the HttpRequest class's default mime type of text/html. 2. It caches parsed templates in its own cache to improve performance. If you aren't using XHTML in your templates, you may choose to comment out the code that tests for and sets the application/xhtml+xml MIME type. I place this code in a file named "util.py" in my application. In my views, I write "from app.util import render_to_response" where I used to write "from django.shortcuts import render_to_response". Note that the caching functionality provided by this code means that you will need to recycle your Django instance when you make template changes. Instructions for doing this depend on how you have deployed your Django application.

  • render_to_response
  • template
  • cache
  • html
  • xhtml
Read More

smoonen has posted 1 snippet.