Login

Most bookmarked snippets

Snippet List

Active page class for selected menu items

Simple tag to check which page we are on, based on resolve: useful to add an 'active' css class in menu items that needs to be aware when they are selected. Typical usage is like: ` <ul> <li class="{% active request "myapp:myview1" %}">My View 1</li> <li class="{% active request "myapp:myview2" %}">My View 2</li> </ul> `

  • templatetag
  • menu
  • tab
Read More

django-thumbs black&white support

Based on the original http://code.google.com/p/django-thumbs/ Added South support too. Usage: ` photo = ImageWithThumbsField(upload_to='images', sizes=((125,125,True),(300,200),)` To retrieve image URL, exactly the same way as with ImageField: `my_object.photo.url` To retrieve thumbnails URL's just add the size to it: `my_object.photo.url_125x125` `my_object.photo.url_300x200` To convert to greyscale set the third attribute of size to True

  • thumbs
  • django-thumbs
  • grayscale
  • black and white
  • thumbsnails
Read More

Unique field inline formset

This method will return an inline formset class that validates values across the given field are unique among all forms. For instance: ApprovedUserFormSet = inlineformset_factory(Request, ApprovedUser, formset=unique_field_formset('email'), form=ApprovedUserForm) Will make sure all ApprovedUser objects created for the Request have unique "email" fields.

  • field
  • unique
  • formset
  • inlineformset
Read More

Alternative to Class Based Views

There is a lot of debate on whether there is a real future for the Django CBVs (class based views). Personally, I find them tedious, and just wanted a way to keep my views clean. So, here is a really minimalistic way of having class based views, without the fuss. This is a fork from: http://stackoverflow.com/questions/742/class-views-in-django http://djangosnippets.org/snippets/2041/

  • views
  • class
  • cbv
  • based
Read More

Syntax highlighting for tracebacks in console output

This is hardcoded to use [django-discover-runner](http://pypi.python.org/pypi/django-discover-runner) since that's my main test runner but could easily be adopted to use Django's own test runner. If you're using a terminal that is capable of showing 256 colors use the `Terminal256Formatter` formatter instead. Enabled it with the `TEST_RUNNER` setting: TEST_RUNNER = 'dotted.path.to.highlighted.runner.HighlightedDiscoverRunner' Where `dotted.path.to.highlighted.runner` is the Python import path of the file you saved the runner in.

  • pygments
  • testing
  • test
  • traceback
Read More

Automatically set up a project

This is a script to automatically set up a django project It takes only one argument for the project name This works for Django 1.4 It will create the following directory structure: /project /server (app name) /media: /html /css /js /img

  • automatic
  • createproject
Read More

Radio widget with labels after inputs

Difference from standard Django 1.4 implementation is just structure of list. In django `<input>` elements are *wrapped* by their labels, and look like this:: <ul> <li><label><input/> Label 1</label> <li><label><input/> Label 2</label> </ul> This widget puts labels *after* input elements:: <ul class="form-button-radio"> <li><input/> <label>Label 1</label> <li><input/> <label>Label 2</label> </ul> It makes possible to define style for both inputs and labels, hiding inputs and making labels look like pressable buttons. No javascript needed, just CSS (see docstring). To auto-submit the form when pressing a button, JQuery can be added:: <script> $(document).ready(function() { $('ul.form-button-radio input[type="radio"]').change(function() { $(this).parents('form').submit(); }); }); </script>

  • widget
  • label
  • input
  • button
  • reorder
  • radio
  • radio button
Read More

template filter to include protocol and domain in absolute urls

I created this template filter to be able to use get_absolute_url in an email template. Save the code into /templatetags/navigation.py Use like this: {% load navigation %} {{ instance.get_absolute_url|siteabsoluteurl:request }}

  • template
  • filter
  • templatetag
  • template-filter
  • url
  • template-filters
  • get_absolute_url
  • templatefilter
  • absolute
Read More

TreePrint: Print nested lists, tuples, dicts

Displays nested lists / dicts / tuples in an aligned hierarchy to make debugging easy. Accepts all variable types, including nested lists with any mixture of tuples / dictionaries / lists / numbers / strings. Analogous to the PHP print_r($var) function.

  • nested
  • nested-set
  • nested-dict
  • print_r
  • nested-list
Read More

Safe template decorator

A decorator that restricts the tags and filters available to template loading and parsing within a function. This is mainly meant to be used when granting users the power of the DTL. You obviously don't want users to be able to do things that could be potentially malicious. The {% ssi %} tag, for example, could be used to display sensitive data if improperly configured. {% load %} gives them access to all the unlimited python code you wrote in your templatetags. {% load sudo %}{% sudo rm -rf / %} o_0 Note that the "load" tag (among others) is not listed in the default tag whitelist. If you parse a template (however indirectly) in a function decorated with this, unlisted builtin tags will behave like undefined tags (ie, they will result in a TemplateSyntaxError). Since {% load %} is not whitelisted, you may want to include some custom tags or filters as "builtins" for convenience. Simply put the module paths to the libraries to include in the `extra` kwarg or the `extra_libraries` list. Generally, this is not recommended, as these libraries need to be carefully and defensively programmed. **NOTE**: This **does not** do anything about cleaning your rendering context! That's completely up to you! This merely restricts what tags and filters are allowed in the templates. Examples: from django.template.loader import get_template safe_get_template = use_safe_templates(get_template) tmpl = safe_get_template('myapp/some_template.html') from django.template import Template use_safe_templates(Template)('{% load sudo %}') # TemplateSyntaxError: Invalid block tag 'load'

  • template
  • clean
  • safe
  • restrict
Read More

3110 snippets posted so far.