Login

Top-rated snippets

Snippet List

Paginator template tag using ObjectPaginator

This template inclusion tag provide a way to have multiple pagination blocks in the same page. Aditionnal parameters in "request.GET" are also automaticaly keeped in pagination links. Usage : **{% show_pagination users_paginator request "page_members" %}** The expected result : **[1] 2 3 … 14** Or : **1 … 5 6 [7] 8 9 … 14**

  • paginator
  • objectpaginator
Read More

Ignore HTTP Accept-Language headers

A little tiny middleware that, when used in multilingual sites, will make Django I18N ignore any `Accept-Language` headers in the request, thus ensuring that every first-time visitor (with no explicit language preference set via session or cookie) will see the site in the language specified by `settings.LANGUAGE_CODE`. (Please note that I think that overriding user preferences is generally a bad practice, but I had my reasons to use it :) )

  • middleware
  • i18n
  • l10n
  • locale
Read More

User or Group entry field & widget

This is a helper field & widget that lets you ask for a 'user or group'. It presents a select box for whether you wish to enter a user or a group, and then a text field for the name of the user or group you wish to have. It will validate that the username or group name selected exists. You would use this like any other field: ` class AddForumCollectionCreatePermForm(forms.Form): user_or_group = UserOrGroupField(label = "User or Group", help_text = "The user or group to grant " "forum collection create priveleges to.") `

  • newforms
Read More

autotranslatslugify

Changes **all slugify calls** to support translat automatically, behind the scenes. Using this one doesn't have to change any models or code to make it work everywhere. Create new project, I call it myself *autoslugifytranslat*, and add the following to project's `__init__.py` file. It will automatically add translat slugify support for all default slugify calls. This script is depending on the fact that slugify function in Django is always in *django.template.defaultfilters.slugify*. **Note:** The snippet is supposed to have "ä","Ä" and "ö","Ö" in the `char_translat` list, but djangosnippets does not let me put ä's and ö's to the code part!

  • i18n
  • slugify
  • translat
  • internal
Read More

Save disk space by hard-linking multiple Django installations

If you work with Django like I do, you have a separate installation or Subversion check-out of Django for each of your projects. Currently each one of them eats 22 MB of disk space. This utility hard-links all identical files between copies of Django. They can even be different versions or svn revisions, and you'll still be able to free a good amount of disk space. Run this every now and then if you update installed copies of Django or add new ones. This utility is available also [here](http://trac.ambitone.com/ambidjangolib/browser/trunk/hardlink_django_instances/hardlink_django_instances.py).

  • disk
  • disk-space
Read More

truncate

Truncates a string after a given number of chars

  • filter
  • templatetags
Read More

Download images as png or pdf

This is an example of how I am providing downloads of dynamic images in either PNG or PDF formats. The PDF format requires ImageMagick's `convert`, and temporary disk space to save the intermediary image. If anyone knows a way to avoid writing to disk, I'd be happy to include it here. I realize there may be uses where it isn't necessary to use the PIL Image class if the image is already stored as a file. This is used for downloading dynamic images without saving them to disk (unless pdf format is used).

  • pdf
  • png
  • custom-response
  • subprocess
Read More

Newforms customs validators

How to proceed to add a custom validator to a newforms field : you just need to create a new class derivated from forms.YourField with a custom clean method. Do not forget the line super(UserField, self).clean(value) ; in our case, it verifies the field attributes : min_length, max_length or required. More explications (in French) : [des validateurs personnalisés pour Django](http://www.aozeo.com/blog/67-django-newforms-validateurs-personnalises)

  • newforms
  • validators
Read More

Url overrides and concurrent site versions

If you want to run multiple versions of a site from the same project IE a staging version and the live one, you need two settings and urlconf files. 1. make separate copies of settings_staging.py and urls_staging.py in the project dir. 2. Change SITE_ID and ROOT_URLCONF in settings_staging.py 3. Make extra include lines in the projects urls_staging.py like the example. 4. Add urls_staging.py to applications where you need extra urls. Make them just like you would normally do urls.py Thanks to ronny for suggesting the double entries in urlconf.

  • urlconf
  • staging
Read More

gettext parachut for python 2.3 with unicode and utf-8

I ran into this because my development system is django on python 2.4 and I deploy to 2.3. It's a corner case where you use gettext, the \# -\*- coding: utf-8 -\*- header and want to have a consistant style in your file. It is encouraged to use the unicode marker like u'string', but this does not work for __str__ methods of your models as they are called by the ``str'' function and that function again can not handle unicode. It would be really nice to have all unicode intern and only do the appropriated encoding on the output. Anyway. With this little helper you can clutter your files with _('stirng of heart with € äüöß') ... With the coding directive in the header python 2.4 and gettext can handle this on 2.3 though they can't. So this script adds a parachut to the gettext wrapper that kicks in if gettext is failing.

  • unicode
  • gettext
  • utf-8
  • 2.4
  • 2.3
Read More

3110 snippets posted so far.