Login

Quickly check templates while sketching them out

Author:
amr-mostafa
Posted:
April 21, 2007
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

This a small but very handy view that gives you a convenient direct access to your templates.

Now suppose you saved the snippet under misc.py, it's critical to add this snippet (or a similar one, once you get the idea) to your urls.py:

if settings.DEBUG:
    # Direct Templates
    urlpatterns += patterns('misc',
        (r'^template/(?P<path>.*)$', 'direct_to_template', {'template': '%(path)s'}),
)

Now you are able to access any of your templates, in different directories by specifying their path after template/. e.g., http://example.com/template/news/index.html

Of course you can change it as you want, you can also add other values to the dict argument, the only required key is 'template'. The whole dict is made available in the template as a context.

All GET parameters are made available in the template too. So http://example.com/template/news/index.html?title=Testing Title will make the {{ title }} var available in your template. So you can substitute basic variables quickly.

This is was inspired by django.views.generic.simple.direct_to_template

1
2
3
4
5
6
7
8
from django.shortcuts import render_to_response
from django.utils.datastructures import MultiValueDict

def direct_to_template(request, template, **kwargs):
    params = MultiValueDict()
    params.update(kwargs)
    params.update(request.GET)
    return render_to_response(template % kwargs, params)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

SmileyChris (on April 23, 2007):

Pretty neat!

Unless you need to pass the template name to your context, you could also just do:

if settings.DEBUG:
    # Direct Templates
    urlpatterns += patterns('misc',
        (r'^template/(.*)$', 'direct_to_template')),
)

#

Please login first before commenting.