Login

Tag "template"

Snippet List

Past days template filter

Returns a list of date objects for a given number of past days, including today. Useful for summaries of recent history. Inspired by [Template range filter](http://www.djangosnippets.org/snippets/1357/)

  • template
  • filter
  • date
Read More

Template tag to clear cached template fragment

This is a custom template tag that clears the cache that was created with the **cache** tag. {% load clearcache %} {% clearcache [fragment_name] [var1] [var2] .. %} Create **app/templatetags** folder with **__init__.py** and copy code into **clearcache.py** file. polls/ templatetags/ __init__.py clearcache.py Based on django.templatetags.cache. See Django docs on [custom template tags](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/)

  • template
  • cache
  • clear
  • fragment
Read More

Variable._resolve_lookup monkeypatch

There are times when you want to hook into the Variable class of django.template to get extra debugging, or even to change its behavior. The code shown is working code that does just that. You can monkeypatch template variable resolution by calling patch_resolver(). I recommend using it for automated tests at first. My particular version of _resolve_lookup does two things--it provides some simple tracing, and it also simplifies variable resolution. In particular, I do not allow index lookups on lists, since we never use that feature in our codebase, and I do not silently catch so many exceptions as the original version, since we want to err on the side of failure for tests. (If you want to do monkeypatching in production, you obviously want to be confident in your tests and have good error catching.) As far as tracing is concerned, right now I am doing very basic "print" statements, but once you have these hooks in place, you can do more exotic things like warn when the same object is being dereferenced too many times, or even build up a graph of object interrelationships. (I leave that as an exercise to the reader.) If you want to see what the core _resolve_lookup method looks like, you can find it in django/templates/__init__.py in your installation, or also here (scroll down to line 701 or so): [source](http://code.djangoproject.com/browser/django/trunk/django/template/__init__.py)

  • template
  • variable
  • resolve
Read More

counter templatetag

The counter initializes the variable to 0, and next it increments one by one: {% load counter_tag %} {% for pet in pets %} {% if pet.is_cat %} {% counter cats %} {% else %} {% counter dogs %} {% endif %} {% endfor %} # cats: {{cats}} # dogs: {{dogs}}

  • template
  • django
  • counter
  • increment
  • loop
Read More

Stacked/Grouped Forms 2 - easy rendering forms

This snippet was inspired by [1783](http://www.djangosnippets.org/snippets/1783/). It allows simply create groups of fields for template rendering.

  • template
  • fields
  • forms
  • templates
  • fieldset
  • form
  • object
  • field
  • fieldsets
  • groups
  • stacked
  • descriptor
Read More

Get sorted items for any model in any order

This get_sorted_items tag takes app.model, a number n, a a field name to sort ,and a variable-name as arguments and will deliver the n first objects of the model. it checks if a Manager *publicmgr* exists and calls this if the user isn't authenticated. Additionally it write the count of the model to the context

  • template
  • tag
  • dry
Read More

Wrappable text

Sometimes you have an uncontrolled amount of text in a horizontally constrained space. The wrappable filter places zero-width breaking spaces into the given text so that it can wrap at any point, as necessary for the containing width. Sometimes better than eliding (chopping long text...) or cropping/scrolling overflow.

  • template
  • filter
  • typography
Read More

Render arbitrary models - template tag

This template tag provides an easy way to render objects in your template, even if you don't know ahead of time what type they are. For example, if you've got a search page with a result list comprised of objects from various models, you can simply loop through them and render them using the tag. The tag will choose the best template and render the object for you. The tag's docstring has all the details. I hope you find this as useful as I have. Questions, comments, complaints welcome.

  • template
  • tag
  • model
  • render
  • display
Read More

TemplateForm

This a mixin that can be used to render forms from predefined templates instead of using .as_table / .as_p / .as_ul

  • template
  • form
Read More

truncatechars filter

Truncates a string after a certain number of chars. Question: > *Why don't you use the built-in filter slice?* I need the "three points" (...) only when it really truncates.

  • template
  • filter
  • truncatewords
Read More

Updated - Template context debugger with (I)Pdb

Tag to inspect template context, filter to inspect variable. Originally by denis, [http://www.djangosnippets.org/snippets/1550/](http://www.djangosnippets.org/snippets/1550/). This just extracts variables from the context to locals for quicker access and autocompletion (When using ipdb). Additionally, 'vars' holds context keys for quick reference to whats available in the template.

  • template
  • debug
  • context
  • pdb
  • ipdb
Read More

Django Template "include_raw" tag

At WWU Housing, we started using the [Tempest jQuery plugin](http://plugins.jquery.com/project/tempest) for javascript templating, which has the same {{ var }} syntax as Django's templating. We wanted to be able to use the same templates in our server-side python and our client-side js, so we had to have a way of including the unrendered template for the js. At the same time, for convenience, it had to be modular so we could push the same code from our dev- to our live-server and not worry about absolute paths (which is why the {% ssi %} tag did not work). So the {% include_raw %} tag was born.

  • template
  • django
  • parse
  • include
  • ssi
Read More

email rendered via javascript (trick spam crawlers)

The proper looking email links rendered to the browser are not in the source code that way, and are rendered by javascript, so robots can't extract it. Its a trick way to have the email displayed properly without getting spamed as a result. Unless the robots are getting smarter, this has worked for me thus far.

  • template
  • email
Read More

improved sortby template tag

Variation on dictsort using attribute access. Nested attributes can be used, like, "obj.attr.attr_attr" Example usage: {% for entry in entries|sortby:'category.title' %} Based on [1609](http://www.djangosnippets.org/snippets/1609/)

  • template
  • tag
  • dictsort
Read More

262 snippets posted so far.