Login

Tag "template"

Snippet List

Using the built-in slugify filter outside a template

This is just a very short (and mostly useless on it's own) example of how the built in slugify filter can be used in a Python script to generate slugs. It was pulled from a script I've written to pull in items from Upcoming.org's API. I post it because "sunturi" posted [Snippet #29](http://www.djangosnippets.org/snippets/29/), which duplicates the functionality of the built-in slugify filter. In the comments of that snippet, santuri (and others) seem to believe that template filters can only be used within templates. This is incorrect, and I think it's important people understand they can be used elsewhere. sunturi's snippet does remove prepositions from values before slugifying them, so if you need that, his code will work work. But if all you need is slugification, the built-in slugify filter will work fine -- in a Python script, as well as in a template.

  • template
  • filter
  • django
  • slug
  • built-in
Read More

getattr template filter

Put inside `mysite/templatetags/getattr.py` Add `mysite` to your `INSTALLED_APPS` In your template: {% load getattr %} {{ myobject|getattr:"theattr,default value" }} Thanks to pterk for optimizations! \\o/

  • template
  • filter
  • get
Read More

Pygments Rendering Template Filter

Finds all ``<code></code>`` blocks in a text block and replaces it with pygments-highlighted html semantics. It tries to guess the format of the input, and it falls back to Python highlighting if it can't decide. This is useful for highlighting code snippets on a blog, for instance.

  • template
  • filter
  • pygments
  • highlighting
  • code
Read More

A couple of template filters for partitioning lists

People -- and by "people" I mean Jeff Croft -- often ask about how to split a list into multiple lists (usually for presenting as columns in a template). These template tags provide two different ways of splitting lists -- on "vertically", and the other "horizontally".

  • template
  • filter
  • lists
Read More

render_to_response wrapper

Simplifies using RequestContext in render_to_response. Simply call the wrapper with request as the first argument, and the rest of the arguments are as normal for render_to_response. ex: render_response(request, 'foo_list.html', {'foo': Foo.objects.all()})

  • render_to_response
  • requestcontext
  • shortcut
  • template
Read More

262 snippets posted so far.