Login

Quickly check templates while sketching them out

Author:
amr-mostafa
Posted:
April 21, 2007
Language:
Python
Version:
.96
Tags:
template view generic
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. Django 1.2+ template loader for Jinja2 by SimonSapin 4 years, 11 months ago
  2. Javascript Chain Select Widget by ogo 6 years, 12 months ago
  3. View decorator to automate templates. by jafo 5 years, 3 months ago
  4. direct to template from a subdir by Scanner 6 years, 7 months ago
  5. Template tag: Extend with parameters by miracle2k 7 years, 7 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.