Login

Tag "tag"

Snippet List

SASS/SCSS include template tag.

You'll need to `pip install pyScss` first. Converts on the fly, so you won't want to use this for much more than just testing. Usage in a template: {% load sass %} {% include_sass "disclosures/css/base.scss" %} {% include_sass "disclosures/css/grid.scss" %}

  • template
  • tag
  • tags
  • scss
  • sass
Read More

User groups template tag

This snippet is an improved version of the [ifusergroup](http://djangosnippets.org/snippets/1576/) tag that allows spaces in any of the group names. It also fixes a small bug where if a group didn't exist none of the subsequent groups would be checked.

  • tag
  • templatetag
  • user
  • auth
  • group
  • permissions
  • access-control
Read More

grouper tag

I needed to display formset into table and I didn“t like solution I have found. So I have written this simple tag you can use it in templates like this: ` {% for row in formset|square_it:6 %} <tr> <td> </td> {% for form in row %} <td> {% for field in form %} {{ field }} {% endfor %} </td> {% endfor %} `

  • template
  • tag
  • templatetag
  • formset
Read More

tag to store a settings value as template variable

Get any value from settings.py as a template variable. The variable can then be used in conditional tags. E.g. to show a link to a help page only if it the help page url is defined in settings.py {% load get_setting %} {% get_setting MY_HELP_URL as help_url %} {% if help_url %}<a href="{% help_url|safe %}">Help</a>{% endif %}

  • template
  • tag
  • templatetag
  • settings
Read More

Querystring Builder - create urls with GET params

Save the code as app_name/templatetags/urlbuilder.py. Supply your view with a dictionary of "default" query params, likely taken from the request. Then, in the template: `{% load urlbuilder %} {% url some-url as base_url %}` That sets you up. Then you can make links. For example: <th><a href="{% build_url base_url query_params sort=name %}">Name</a></th> Say query_params has: page: 2, filter: active, sort: id The above tag would spit out /base/url?page=2&filter=active&sort=name The tag also has support for using a variable as the replacement key. Say "filter_key" is a variable available to the template with the value "filter": {% build_url base_url query_params filter_key default %} Using the above example, that would output /base/url?page=2&filter=default&sort=id

  • tag
  • templatetag
  • url
  • parameter
  • querystring
  • GET
  • param
Read More

Image inlining template tag lib

A custom templatetag for inlining image in the browser. The principe is to base64 encode the image and avoid a http request. There is a cache handling, you just have to specify a writable directory. An example of the utilisation (template part): [http://djangosnippets.org/snippets/2267/](http://djangosnippets.org/snippets/2267/) The explication on [http://raphaelbeck.wordpress.com/2010/11/14/make-inline-images-to-improve-performance-with-django-template-tags/](http://raphaelbeck.wordpress.com/2010/11/14/make-inline-images-to-improve-performance-with-django-template-tags/)

  • tag
  • image
  • templatetag
  • base64
Read More

Image inlining template tag example

The example of the Image inlining template tag lib which inline images in the browser instead of making an Http request. The lib is available at : [http://djangosnippets.org/snippets/2268/](http://djangosnippets.org/snippets/2268/)

  • tag
  • image
  • templatetag
  • base64
Read More

Improved many-page pagination

This one was adapted from [Page numbers with ... like in Digg](http://djangosnippets.org/snippets/1441/). See that one for more reference. Digg-like page numbering using inclusion tag. Usage in template: `{% load pagination %} {% pagination yourpage %}` Inclusion template pagination.html: {% if page.has_previous %} <a href="?page={{ page.previous_page_number }}"><img src="{{ MEDIA_URL }}images/page/cyclenav_l.png" alt="Previous page" /></a> {% endif %} {% for pnum in begin %} <a href="?page={{ pnum }}"{% if page.number == pnum %} class="active"{% endif %}>{{ pnum }}</a> {% endfor %} {% if middle %} <strong>...</strong> {% for pnum in middle %} <a href="?page={{ pnum }}"{% if page.number == pnum %} class="active"{% endif %}>{{ pnum }}</a> {% endfor %} {% endif %} {% if end %} <strong>...</strong> {% for pnum in end %} <a href="?page={{ pnum }}"{% if page.number == pnum %} class="active"{% endif %}>{{ pnum }}</a> {% endfor %} {% endif %} {% if page.has_next %} <a href="?page={{ page.next_page_number }}"><img src="{{ MEDIA_URL }}images/page/cyclenav_l.png" alt="Previous page" /></a> {% endif %}` Produces: previous_img 1 2 ... 4 5 6 7 8 9 10 11 12 ... 17 18 next_img Or: 1 2 3 4 5 6 7 8 ... 17 18 next_img Or: previous_img 1 2 ... 10 11 12 13 14 15 16 17 18 next_img

  • tag
  • django
  • templatetag
  • pagination
  • digg
  • pages
Read More

Parse custom template tag's args or kwargs

Enhanced version of snippet [1113](http://djangosnippets.org/snippets/1113/) Usage example (not self-contained): @register.tag def groupurl(parser, token): ''' Syntax:: {% groupurl view_name group [key=val, [key=val, ...]] [as varname] %} Example:: <a href="{% groupurl blog_detail group slug=blog.slug %}">{{ blog.name }}</a> ''' bits = token.contents.split() tag_name = bits[0] if len(bits) < 3: raise template.TemplateSyntaxError("'%s' takes tag at least 2 arguments (path to a view and a group)" % (tag_name,) bits_iter = iter(bits[1:]) # view_name + group and url kwargs args, kwargs = parse_args_and_kwargs(parser, bits_iter, stop_test='as', tagname=tag_name) if len(args) != 2: raise template.TemplateSyntaxError("'%s' takes exactly two non-kwargs (path to a view and a group)" % (tag_name,)) view_name, group = args # as var asvar = None for bit in bits_iter: asvar = bit return GroupURLNode(view_name, group, kwargs, asvar)

  • tag
  • templatetag
  • parse
  • args
  • kwargs
Read More

Template Tag for Random Selection of Any Line

These are template tags meant to support the construction of text in a random or seeded random (reproducible) way. Two tags are provided: `seed_randomization` and `any`. Only seed the randomization if you wish to have the options generated the same way each time. Only necessary once per request, if done early enough in the rendering process. Example without seeding: <p> {% any %} One day Once upon a time In a galaxy far, far away {% endany %} a young foolish {% any %}programmer|lawyer|Jedi{% endany %} {% any %} set out began his quest ran screaming {% endany %} to pay his stupid tax. </p> # Possible outcomes: <p>In a galaxy far, far away a young foolish lawyer set out to pay his stupid tax.</p> <p>One day a young foolish programmer ran screaming to pay his stupid tax.</p> Be sure to read the documentation in the code.

  • tag
  • django
  • random
Read More

Add GET parameter tag

This is custom tag I wrote for myself for solving situations when you have filter form and page numbers in the same page. You want to change ?page=.. or add it if it doesn't exist to save filter form data while moving through pages. Usage: place this code in your application_dir/templatetags/add_url_parameter.py In template: {% load add_get_parameter %} <a href="{% add_get_paramater param1='const_value',param2=variable_in_context %}"> Link with modified params </a> It's required that you have 'django.core.context_processors.request' in TEMPLATE_CONTEXT_PROCESSORS URL: [http://django.mar.lt/2010/07/add-get-parameter-tag.html](http://django.mar.lt/2010/07/add-get-parameter-tag.html)

  • tag
  • parameters
  • GET
  • add
  • add_get_parameter
Read More

Yet Another Image Resizer

There's a whole range of examples online for resizing images in Django some of which are incredibly comprehensive with a wide variety of options. Here's my take on the task that serves as a simple drop in for when you don't want to include a separate app. - Only generates the resized image when first requested. - Handles maintaining proportions when specifying only a width or a height. - Makes use of PIL.ImageOps.fit for cropping without reinventing the wheel.

  • template
  • tag
  • image
  • pil
  • thumbnail
  • resize
Read More

119 snippets posted so far.