Login

All snippets written in Python

Snippet List

whitespaceoptimize block tag

This is a custom block tag and is used like this: {% load whitespaceoptimize %} {% whitespaceoptimize "css" %} /* CSS comment */ body { color: #CCCCCC; } {% endwhitespaceoptimize %} And when rendered you get this output: body{color:#CCC} To install it, download the snippet and call it `myapp/templatetags/whitespaceoptimize.py`. (Make sure you have a `__init__.py` in the `templatetags` directory) You need to download and install [slimmer](http://www.issuetrackerproduct.com/Download#slimmer) and put on your Python path. The block tag can be used for `javascript` and `html` as well as `css`. You can also let it guess the format; this would work for example: {% whitespaceoptimize %} <table> <tr> ... {% endwhitespaceoptimize %} ...but this would just replicate the functonality of the [built-in spaceless tag](http://docs.djangoproject.com/en/dev/ref/templates/builtins/#spaceless)

  • tag
  • block
  • whitespace
  • slimmer
Read More

Orderable inlines using drag and drop with jQuery UI

An easy way of making inlines orderable using drag-and-drop, using [jQuery UI's](http://ui.jquery.com/) sortable() plugin. First, add an "order" field to the inline models which is an IntegerField, and set that model to use 'order' as its default order_by. Then hook in the JavaScript. This should make them drag-and-drop sortable using jQuery UI, and also hide the divs containing those order fields once the page has loaded. This technique is unobtrusive: if JavaScript is disabled, the order fields will be visible and the user will be able to manually set the order by entering numbers themselves.

  • jquery
  • inlines
  • pyconuk2008
Read More

keeping Model and Field History (everywhere)

Usage: class MyModel(ModelWithHistory): class History: model = True # save model changes into admin's LogEntry table fields = ('f1', 'f2') # save these fields history to AttributeLogEntry table f1 = CharField(max_length=100) f2 = IntegerField() for threadlocals, see http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser Aware! Not thoroughly tested yet. May cause problems with loading fixtures.

  • model
  • history
Read More

Alphabetic filter for admin

This snippet is based on [#748](http://www.djangosnippets.org/snippets/748/). Adds filtering by first char (alphabetic style) of values in the admin filter sidebar. The example below results in this filter: By name that starts with All A B G M X urls.py example (only for register the filter): import <your project>.admin.filterspecs models.py example: from django.db import models class Person(models.Model): name = models.CharField(max_length=40) name.alphabetic_filter = True admin.py example: class Admin: list_filter = ['name']

  • filter
  • admin
  • filterspec
Read More

FreeBSD rc.d FastCGI Script

This is a simple rc script, suitable for starting a FastCGI daemon on FreeBSD. Simply copy this into /usr/local/etc/rc.d , change all references to "signet" to the name of your app, mark it executable and modify /etc/rc.conf accordingly.

  • fcgi
  • freebsd
  • fastcgi
  • rc.d
  • rc
Read More

Retrieve a list of countries from GeoNames

GeoNames provides a useful data file with information about every country in the world (DjangoPeople uses this). Here's an importer that grabs the file from the web and turns it in to a list of dictionaries.

  • countries
  • geonames
Read More

autotranslate po files using google translate

Save to autotranslate.py, run using python autotranslate.py pofile inputlang outputlang, eg. python autotranslate.py path_to_blank_fr_lang.po en fr, to translate to french. Some known bugs: * Doesn't handle some line returns properly * Block translations aren't formated correctly in the translation. If anyone has any issues or fixes please post to the comments. Of course the output shouldn't be used as substitute to a proper translation.

  • translation
Read More

WorldIP - access to IP database over API

The WorldIP database provides real-world geographical location. Database is more correct than [Whois records and Whois-based databases](http://www.wipmania.com/en/blog/why-worldip-data-rather-than-whois-data-examples/), that show geographic locations of network owners, and not the geographic location of Internet-connected PC or appliance itself. See more: [WIPmania.com](http://www.wipmania.com)

  • ip
  • geolocation
  • geodjango
  • worldip
Read More

local django model test

Sometimes you need to test some model features without a complete django app installation. Just play only with the model object. With these small script you have a complete in memory django installation. Some Links: http://www.djangosnippets.org/snippets/1044/ (en) http://www.jensdiemer.de/permalink/150/mein-blog/99/django-db-model-test/ (de) http://www.python-forum.de/viewtopic.php?f=3&t=15649 (de) See also: https://github.com/readevalprint/mini-django/

  • model
  • testing
  • test
  • local-test
Read More

Bind Administration

Binds $Model to $ModelAdmin without having to specify each binding manually. The ModelAdmins **must** have the same name as the model (as well as same case) with Admin appended. Example: from django.db import models class SomeModel(models.Model): name = models.CharField(max_length=255) class AnotherModel(models.Model): name = models.CharField(max_length=255) # bind administration bind_administration('myproject.myapp.models', 'myproject.myapp.admin')

  • admin
  • 1.0
  • modeladmin
Read More

Improved Accept header middleware

A clean and simple implementation of parsing the Accept header. It places the result in request.accepted_types. Place this middleware anywhere in the chain, as all it does is add to the request object.

  • middleware
  • accept
  • header
Read More

Decorator cache handler per view

The @cache_holding decorator works in a per-function base, you can specify a list of models or just a model and the second argument is the time in seconds to be cached. You can modify the proxy class by a keyword argument so you can do cache to all kind of things, also if you want to use a prefix as key + the hash value you can specify the prefix keyword argument.

  • cache
  • view
  • decorator
Read More

2955 snippets posted so far.