Parsed RSS into template var
loads a parsed RSS feed (with feedparser) into a variable of choice. Caching by the "cache" template tag.
- template
- tag
- templatetag
- cache
- rss
- parse
loads a parsed RSS feed (with feedparser) into a variable of choice. Caching by the "cache" template tag.
This snippet provides support for dynamically importing templates. This helps you to avoid naming collisions and other problems. The format is as follows: 1. `module.submodule.app:template.html` 2. `module.submodule.app:subfolder/template.html` 3. `module.submodule.app:/subfolder/template.html` Assuming the module is located in '/var/', these would map (respectively) to: 1. `/var/module/submodule/app/templates/template.html` 2. `/var/module/submodule/app/templates/subfolder/template.html` 3. `/var/module/submodule/app/templates/subfolder/template.html` The colon splits the the python module from the template directory, meaning you can import from anything that has a "templates" directory. This helps me to avoid naming collisions by specifying the application I'm referring to, without having to put long paths in my extends and include tags inside other templates. It's also dynamic in that if I move a library outside the old path, it has no effect on the templates. To get this rolling, in your `settings.py` file, add the following:: >TEMPLATE_LOADERS = ( > 'addons.template_loader.load_template_source', # <--- add this > 'django.template.loaders.app_directories.load_template_source', > 'django.template.loaders.filesystem.load_template_source', ># 'django.template.loaders.eggs.load_template_source', >)
Allows you to search if a user belongs to a given group. Along the same lines as snippet [390](http://www.djangosnippets.org/snippets/390/), but uses a regular ``if`` tag so it is more flexible. (Updated for efficiency. Running a boolean test on a QuerySet avoids a bit of unnecessary overhead.) (Updated to accept a list of groups.)
I couldn't find any code for a blog-style "Read more after the jump," so I made a custom filter. It will look for **<!--more-->** for the jump, like in Wordpress. In **settings.py** set **READ_MORE_TEXT** to what you want the text of the link to be. `READ_MORE_TEXT = 'Read more after the jump.'` When you call the filter in your template, pass it the absolute link of that entry. Of course, you have to have your **get_absolute_url** function defined in your model, but you should have that already, right? :P In this example **entry.body** is the content of the blog entry. `{% load blog_filters %}` `{{ entry.body|read_more:entry.get_absolute_url }}` If anyone has a better way to do this, it is, of course, welcome.
This describes an issue with the template system that people may come across.
Template: `<div id="messageBox_{{ forloop.counter0 }}" style="border:1px solid #ccc;background-color:white" onclick="document.getElementById ('messageBox_{{ forloop.counter0 }}').style.display = 'none';"> {% ifstartswith message "#ok#" %} <font color="green"> {% endifstartswith %} {% ifstartswith message "#error#" %} <font color="red"> {% endifstartswith %} {{ message|cut:"#ok#"|cut:"#error#" }} </font> </div>` In a view you can now do something like that: ` request.user.message_set.create(message="#ok#Hello User, this is a ok message!")`
This tiny template filter saves you the tedious test "if this variable is set, print this text based on this variable". 'verbose' filter takes one parameter : a string containing '%s' which is a placeholder for the value to test. Check those examples : * Replace this : {% if name %} Hello {{ name }}, this is a dummy text {% endif %} * By this : {{ name|verbose:"Hello %s this is a dummy text" }} This is also usefull for HTML : {{ image|verbose:"<img src=\"%s\" />" }}
This is a simple tag that I am sure has been written before, but it helps people with the problem, 'how do I iterate through a number in the tempaltes?'. Takes a number and iterates and returns a range (list) that can be iterated through in templates Syntax: {% num_range 5 as some_range %} {% for i in some_range %} {{ i }}: Something I want to repeat\n {% endfor %} Produces: 0: Something I want to repeat 1: Something I want to repeat 2: Something I want to repeat 3: Something I want to repeat 4: Something I want to repeat
Allows getting the rendered content of a specific block tag. Useful if you want to send just a part of a template back for an AJAX request. Works for arbitrary template inheritance, even if a block is defined in the child template but not in the parent. Example: In `test1.html`: {% block block1 %}block1 from test1{% endblock %} {% block block2 %}block2 from test1{% endblock %} In `test2.html`: {% extends 'test1.html' %} {% block block1 %}block1 from test1{% endblock %} And from the Python shell: >>> from django.template import loader, Context >>> from template import render_block_to_string >>> print render_block_to_string('test2.html', 'block1', Context({})) u'block1 from test2' >>> print render_block_to_string('test2.html', 'block2', Context({})) u'block2 from test1' UPDATE: See also [zbyte64](http://www.djangosnippets.org/users/zbyte64/)'s implementation in snippet [#942](http://www.djangosnippets.org/snippets/942/)
**Attention: this snippet depends on jQuery library to works** The Admin templates hierarchy does not allow you change the submit line (that buttons bar in the footer of the change view of a model class). This code shows how to resolve this, using a simple javascript trick. You can read the code and implement using your favorite javascript library (instead of jQuery) or pure JavaScript. This example regards to User Profile, a common used model class that adds custom fields to a user in the authentication system. To use this, you must put this template code in a file with the following name: templates/admin/auth/user/change_form.html
This module has two template filters allowing you to dump any template variable. Special handling for object instances. Pretty output. Usage: {% load dumper %} ... <pre>{{ var|rawdump }}</pre> or {% load dumper %} ... {{ var2|dump }} How to install: As usual, put into `<your-proj>/<any-app>/templatetags/dumper.py`.
put this code into a file in a folder "templatetags" inside some application and use it like this: {% load your_file_name %} {% for item in your_list|order_by:"field1,-field2,other_class__field_name"
Sometimes we want to get a queryset or 404, for example, if we're passing our queryset to a subclassed generic view. This is identical to get_list_or_404 but returns a queryset.
You can add this code to a file named "field_attrs.py" in a templatetags folder inside an application. To use it, remember to load the file with the following template tag: {% load field_attrs %} And for each field you want to change the widget's attr: {{ form.phone|attr:"style=width:143px;background-color:yellow"|attr:"size=30" }}
Usage: {% mailto email [linktext] %} `email' parameter is required; linktext is optional, defaulting to the email address.
262 snippets posted so far.