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/)
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/)
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)
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}}
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
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.
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.
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.
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.
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 tag to obfuscate emails and other spam sensitive information.
Usage
obfuscate '[email protected]' 'Link display' 'Link title'
obfuscate '[email protected]' 'Link display'
obfuscate 'phone number'
Renders complex xhmtl compliant javascript.
May need caching as it renders a new code every time.
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.
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/)