Login

Top-rated snippets

Snippet List

PermanentRedirectMiddleware

This is a simple middleware that redirects the exactly URL requested with the correct domain. It is useful when you have more than one domain (most of cases with "www." or IP) to access a website. To make it works, download the snippet file as the name "permanent_redirect.py" and add its path as the first item in MIDDLEWARE_CLASSES setting in settings.py. Later you must inform a setting called `HTTP_HOST_DOMAIN` with the correct domain.

  • middleware
  • redirect
  • permanent
  • seo
Read More

Creator/updater fields for admin

This ModelAdmin class sets fields for models saved in admin corresponding to the user that created the object and the user that last updated the object. Trivial for the current model, but a little more involved to make it work with inlines. The fields still show up as drop-downs (`select`) in the admin, but I fixed that with a little jQuery: $(function(){ $("select[id*='creator'], select[id*='updater']").each(function(){ var user = $('option:selected', this).text(); $(this).siblings('.add-another').hide(); $(this).hide(); $(this).after(user); }); }); This could easily be subverted, but with trusted users, it makes for a quick and dirty read-only field.

  • admin
  • newforms-admin
Read More

Ordering a queryset by _CHOICES

I recently needed to sort a list of objects by cardinal direction clock-wise. Since this is different than alphabetical, and I didn't want to use a dictionary to map to integers, here is what I came up with. There may be a cleaner way to do this by overriding some object methods, but I just thought I'd put this out there anyway.

  • choices
  • ordering
Read More

Custom color field with Javascript color picker

A custom model field 'ColorField' which stores a hex color value like '#FFFFFF' and shows a Javascript color picker in the admin rather than a raw text field. It is written to work with the current trunk (i.e. after newforms-admin merge). You'll need the ColorPicker2.js file found at [www.mattkruse.com](http://www.mattkruse.com/javascript/colorpicker/combined_compact_source.html) (his license prohibits including the file here). This should be placed in the 'js' folder of your admin media. The snippet includes a python source file which can be placed wherever you wish, and a template which by default should be placed in a folder 'widget' somewhere on your template path. You can put it elsewhere, just update the path ColorWidget.render The custom field at present does not validate that the text is a valid hex color value, that'd be a nice addition.

  • newforms
  • javascript
  • models
  • admin
Read More

Translated choices fields

- Choices are saved as the key integers. - Admin will show the correct translation in forms. - You can reuse the make_choices function for other choices fields. - Bad side: bin/make_messages.py won't get the choices values automatically, you have to add them in the .po's by hand.

  • models
  • choices
  • i18n
Read More

LDAP to Django Synchronization

I needed to be able to synchronize my LDAP users and groups to the Django database. This may not be as efficient as some might like but it works like a charm. It returns a list of messages that I pipe into request.user.messages in my template.

  • user
  • auth
  • ldap
  • group
Read More

Display arbitrary models

Template tag for displaying a list of arbitrary models. Useful for life-stream kind of pages that display blog entries, links, photos etc ordered by date. [Example](http://bjornkri.com) **Usage:** something like: {% for object in object_list %} {% display_excerpt object %} {% endfor %} Will look for *app/model_excerpt.html* by default, and fall back on a generic *display_excerpt.html*, or returns the object's string representation as a last fallback. *display_excerpt.html* might look something like: <a href="{{ object.get_absolute_url }}">{{ object }}</a> Any model you throw at it should have a *get_absolute_url* and a string representation of some sort, so this gives you the bare minimum of a title and a link to a detail page. *display_excerpt* takes an optional argument to set the template suffix. This might be handy for generating different formatting for feeds, for instance: {% for object in object_list %} {% display_excerpt object "feed" %} {% endfor %} This will look for app/model_feed.html to render the object. Got lots of help from mattmcc on #django for this one, thanks!

  • display
  • excerpt
  • lifestream
Read More

upload handler decorators

In an admin custom view I had the requirement to modify the upload handlers. However, the @staff_member_required locked the Files upload handlers as it uses the request.POST - see [Ticket 7654](http://code.djangoproject.com/ticket/7654). These decorators can be used before other decorators to allow setting of the upload handlers. Usage example: @upload_handlers_insert(0, QuotaUploadHandler) @staff_member_required def upload(request): pass

  • admin
  • decorator
  • file
  • uploadhandler
Read More

Search Docs with Shortwave

. . For use with [Shortwave](http://shortwaveapp.com/). Add this to your hosted `wave.txt` for a search of the current Django docs.

  • search
  • documentation
  • shortwave
Read More

Last.fm Charts

This will fetch the Top Artists List for the given username from Last.fm. It makes use of the django.cache Framework. I use it with django 0.96.2. Enjoy! Usage: ` {% load lastfm %} {% lastfm_topartists YourName as topartists %} {% for artist in topartists %} {{ artist.thumbnail }}, {{ artist.name }}, {{ artist.url }}, {{ artist.playcount }} and so on {% endfor %} `

  • template
  • chart
  • lastfm
Read More

Restrict Middleware

This is a _very basic_, _easily foolable_, restriction method implemented in a Django middleware. However, for low security sites that need a cursory barrier to entry (without the ability to assign/administer user accounts), this does very well. All of the features are fairly well-documented in the code.

  • whitelist
  • restrict
  • reject
Read More

Google Charts Templatetags (Python)

The core templatetags for my project [google-chartwrapper](http://code.google.com/p/google-chartwrapper/). It is an easy method of creating dynamic GoogleCharts from the [GoogleChartAPI](http://code.google.com/apis/chart/). To get the most recent version: `svn checkout http://google-chartwrapper.googlecode.com/svn/trunk/` and run `python setup.py` in the downloaded trunk directory. There is an included django project there with the [ChartsExamples](http://code.google.com/p/google-chartwrapper/wiki/ChartExamples) all worked out in django templates

  • templatetags
  • google
  • chart
Read More

3110 snippets posted so far.