Login

Tag "template"

Snippet List

Random-image template tag

This tag makes it easy to have a random rotation of images on a page. Don't forget to set your MEDIA_URL.

  • template
  • image
  • random
Read More
Author: pbx
  • 3
  • 12

django-tagging clouds template tag

template tag for producing tag clouds for a given model, using django-tagging application: http://code.google.com/p/django-tagging/ Sample: http://skam.webfactional.com/tags/ Usage: {% tags_cloud_for_model articles.Article as object_list %} {% tags_cloud_for_model articles.Article as object_list step 6 %}

  • template
  • tag
  • tags
  • tagging
  • templatetags
  • clouds
Read More
Author: skam
  • 13
  • 59

WordWrap template tag

Basically a clone of the default "wrap" filter. I use it to generate plaintext e-mail that has to be broken at 75 characters. {% wordwrap 80 %} Some text here, including other template tags, includes, etc. {% endwordwrap %} I prefer this over the {% filter wordwrap:80 %} as my template (e-mail) writers keep screwing it up.

  • template
  • tags
  • wordwrap
Read More

{% with %} template tag

Add a value to the context (inside of this block) for easy access. Provides a way to stay DRYer in your templates. **NOTE:** This tag is now in Django core, so if you have Django >0.96 (or SVN) then you do NOT need this.

  • template
  • template-tag
  • encapsulation
Read More

PyIf Template Tag (Conditional Tag)

Cheers to limodou for getting me thinking about this. The only problem with his implementation is that it doesn't support Django's "." syntax for accessing array/dict elements. In the Django style of allowing simple syntax for designers while allowing for greater flexibility, and less template duplication for conditionals that were previously impossible to represent in templates, I modified Django's built-in If tag. This is an adaptation/enhancement to Django's built in IfNode {% if ... %} that combines if ifequal ifnotequal into one and then adds even more. This Supports 1. ==, != 2. not .... 3. v in (1,"y",z) 4. <=, <, >=, > 5. nesting (True and (False or (True or False))) How to use it: {% pyif i == 1 or (5 >= i and i != 7) and user.first_name in ('John', 'Jacob') %} 'Tis true. {% else %} 'Tis false. {% endif %} I hope you like it.

  • template
  • tag
  • templatetag
  • if
  • conditional
  • ifequal
  • ifnotequal
Read More

Calendar template-tag

Simple template tag to show a calendar. I use it to display events (which is a model with a start_date and end_date attribute. You probably should change this according to your needs.

  • template
  • tag
  • calendar
Read More

TemplatedForm

Newforms are made to make any kind of customizations easy. Sometimes standard methods of rendering HTML/XML/other content of the django.newforms is not enough to completely satisfy all web design needs so you may want to present forms in own templates. Using templates to render output as HTML or XML is straighforward, and in many cases easier then using standard approach. Step by step usage guide: 1. Create file in your project yourproject/utils/newforms.py and place Class TemplatedForm there 2. Create newforms subdirectory in templates dir, and post 2 templates there (form.html, field.html) from the documentation code for TemplatedForm class 3. Inherit from TemplatedForm in your form class to use this form.

  • template
  • newforms
Read More

Convert LaTeX templates to various output formats

**The code is a bit messy and may include bugs ;-) Anyway, I want to clean it up and add features (see below).** The `process_latex()` function will read the given template, render it with a custom context and convert it into the desired output format by running it through pdflatex. Depending on the `outfile` parameter, the output will either be returned (for a response object) or written to the path specified in `outfile` (useful for `save()` in models which generate static, unique files). **TODO** * Error handling in case pdflatex breaks (non-zero return value) * Optionally returning a HttpResponse with the correct mimetype and Content-Disposition set to attachement and an appropriate filename * RequestContext instead of Context (passing the Context object instead of a dict) **CREDITS** Credits also go to ronny for the names dict :-P

  • template
  • django
  • pdf
  • dvi
  • png
  • latex
  • pdflatex
Read More

intergrate mako and genshi with django

You can download these files from [here](http://huangyilib.googlecode.com/svn/trunk/mashi_django) Django template, mako, genshi, they are the best three templates in python, aren't they? How to use these files ? [Using Mako in Django](http://fuzzythinker.blogspot.com/2007/04/using-mako-in-django.html) -- by John Leung

  • template
  • mako
  • genshi
Read More

Template Query Debug

I often find something like this lurking at the end of my base templates - it'll show you which queries were run while generating the current page, but they'll start out hidden so as not to be a pain. Of course, before this works, you'll need to satisfy all the criteria for getting debug information in your template context: 1. Have `'django.core.context_processors.debug'` in your `TEMPLATE_CONTEXT_PROCESSORS` setting (it was there in the default settings, last time I checked). 2. Have your current IP in your `INTERNAL_IPS` setting. 3. Use [RequestContext](http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext) when rendering the current template (if you're using a generic view, you're already using `RequestContext`).

  • template
  • debug
  • queries
Read More

Render dynamically assigned fields in a template

**Problem:** You want to render an arbitrary number of fields assigned dynamically, as in [snippet #27](http://www.djangosnippets.org/snippets/27/), but using multiple `if` statements in a template would be tedious. **Solution:** newforms BoundField The example demonstrates a form with medication fields. We don't know in advance how many meds will be prescribed to a patient, but we want to display a minimum of 4 medication fields, each consisting of a label, name, and dosage. My code uses a cheap hack of assigning a .bf attribute to the fields during __init__, but it's easy to render in a template loop: `{% for med in form.med_list %}` *Special thanks to Honza for advice on BoundField.*

  • template
  • dynamic
  • boundfield
  • render
Read More

Paginator Tag

Piggybacks on the pagination-related template context variables provided by the `object_list` generic view, adding extra context variables for use in displaying links for a given number of pages adjacent to the current page and determining if the first and last pages are included in the displayed links. Also makes it easy to implement consistent paging all over your site by implementing your pagination controls in a single place - paginator.html. Optionally accepts a single argument to specify the number of page links adjacent to the current page to be displayed. Usage: `{% paginator %}` `{% paginator 5 %}`

  • template
  • tag
  • pagination
Read More

Find all links in a value and display them separatley

This is a simple filter I use to display a list of links from a blog entry off in the sidebar ([example](http://www2.jeffcroft.com/blog/2007/feb/25/two-new-django-sites-both-source-available/)). Requires beautifulsoup. Originally by [Nathan Borror](http://playgroundblues.com), tweaked slightly by me.

  • template
  • filter
  • beautifulsoup
Read More

Using the {% widthratio %} template tag with CSS to create a bar graph

The {% widthratio %} template tag is under appreciated! Here, it's combined with CSS to create a bar graphic for the results of an election (this example comes from [this page](http://flickr.com/photos/postneo/405239750/in/photostream/), but has been modified slightly for simplicity's sake). The widthratio tag can be used to create all sorts of graphs and charts, as well as things like tag clouds. Here, we pass it the number of votes for a candidate, the total number of votes in the election, and the integer 190, which is the width, in pixels, of a "full" bar on the bar graph. In other words, 100% = 190 pixels. It works great!

  • template
  • templatetag
  • widthratio
  • graph
  • infographic
Read More

262 snippets posted so far.