Login

All snippets written in Python

Snippet List

Expire page from cache

A simple helper function that clears the cache for a given URL, assuming no headers. Probably best used when wired up to a model's post_save event via signals. See [message to django-users mailing list](http://groups.google.com/group/django-users/msg/b077ec2e97697601) for background.

  • cache
  • caching
  • expiration
Read More

Super User Conditional Page Exception Reporting

**Step 1** Save somewhere in your project directory **Step 2** Add to your settings.py MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.doc.XViewMiddleware', 'utils.debug.UserBasedExceptionMiddleware', ) Normal users will get your 500.html when debug = False, but If you are logged in as a super user then you get to see the stack trace in all its glory.

  • middleware
  • debug
  • exception
Read More

Admin Image Widget

A FileField Widget that displays an image instead of a file path if the current file is an image. Could also be used with sorl.thumbnail to generate thumbnail images. **Example** class FileUploadForm(forms.ModelForm): upload = forms.FileField(widget=AdminThumbnailWidget) class Meta: model = FileUpload class FileUploadAdmin(admin.ModelAdmin): form = FileUploadForm admin.site.register(FileUpload, FileUploadAdmin)

  • image
  • newforms-admin
  • widget
  • file
  • nfa
Read More

Pledgie data parser

This one uses - and is very similar to - http://www.djangosnippets.org/snippets/852/ It gets the data from a pledgie.com campaign and parses it.

  • pledgie
  • donation
  • parser
Read More

SessionMessages

Creates a message list, but unlike the messaging system in django.contrib.auth it is session-persistent and can therefor be displayed after a redirect.

  • messages
  • sessionstore
Read More

Update All Apps to Latest Revision

This snippet is based on [#844](http://www.djangosnippets.org/snippets/844/ "#844") and [#892](http://www.djangosnippets.org/snippets/892/ "#892") and updates all apps in the current directory using hg, svn, git or bzr. Including subdirectories not under version control (subfolders to keep your stuff organized). For example: python/lib/ django-trunk/ django-0.96/ pydelicious/ (...) django-apps/ django-tagging/ django-pagination/ django-registration/ django-threadedcomments/ django-mptt/ (...) The script will iterate through all of your apps (in the current dir and also recursively in subdirs NOT under version control) and update them to the latest version. To run, simply execute: python update_apps.py in the desired parent folder. Just in case it could be useful: In my case I'm using MAC OS X. I have a folder full of miscellaneous scripts under my HOMEDIR, with this content: /Users/Dedaluz/bin/update_apps.py /Users/Dedaluz/bin/update_apps (this is a bash script) The update_apps script contains simply: #!/bin/bash python /Users/Dedaluz/bin/update_apps.py Then I put this folder in my path, so in my /HOMEDIR/.bash_profile I add this line export PATH=$PATH:$HOME/bin And I just can update from any parent folder just going there and typing: update_apps

  • script
  • update
  • svn
  • git
  • hg
  • bzr
Read More

syncdata command

A django admin command that takes a fixture and makes the target database the same as that fixture, deleting objects that in the database but not in the fixture, updating objects that are different in the database, and inserting missing ones. Place this code in your_app/management/commands/syncdata.py You will need to use manage.py (not django-admin.py) for Django to recognise custom commands (see http://www.djangoproject.com/documentation/django-admin/#customized-actions). This snippet is the 'loaddata' command with this patch applied: http://code.djangoproject.com/ticket/7159 (with minor tweaks). The intention is that 'dumpdata' on system A followed by 'syncdata' on system B is equivalent to a database copy from A to B. The database structure in A and B must match.

  • admin
  • database
  • import
  • commands
  • copy
  • command
  • synchronise
  • publish
Read More

[middleware] Rewrite anchors to point into Coral CDN

This simple middleware rewrites the 'href' attribute of any `<a>` tags in your response content. The URL href is modified by appending the string '.nyud.net', which causes the Coral Content Distribution Network to retrieve a copy of the page and cache it before returning it to the user agent. This might be useful if you're writing another Slashdot and you want to avoid turning the servers you link to into smoking craters. You [should be able](http://www.djangosnippets.org/snippets/910/) to apply this functionality to a single view as well (though I haven't tried this yet).

  • middleware
  • coral
  • cdn
  • anchor-rewrite
Read More

Serializing booleans correctly when doing dumpdata from a MySQL database using Django 0.96

Django 0.96 seems to have a bug when serializing from MySQL. BooleanFields are encoding as 0/1 instead of true/false. Hacking the python serializer seems to fix that. The bug shows up as (fx. when using loaddata on a dump from MySQL in PostgreSQL): Problem installing fixture '/tmp/data.json': ERROR: column "is_staff" is of type boolean but expression is of type integer HINT: You will need to rewrite or cast the expression.

  • dumpdata
  • 0.96
  • seralization
Read More

isUnique validator for newforms

This is a generic unique field value validator for use with newforms. ( It's handy to plug into newforms-admin.) Example, with newforms-admin: ` class LinkAdminForm( ModelForm ): def clean_url( self ): return isUnique( self.instance, 'url', self.cleaned_data['url']) class LinkAdmin( ModelAdmin ): form = LinkAdminForm site.register( Link, LinkAdmin ) `

  • newforms
  • admin
  • unique
Read More

Load a local settings file for dev/test environments

Add the snippet to your settings.py. If you have a settings_local.py it will load that one. Can be used in development environments where you might have different settings for your dev sandbox. You should exclude settings_local.py from SVN. By Rudy and Ed Menendez

  • settings
  • environment
  • production
Read More

Delete View

Usage: @login_required def action_delete(request,object_id): return delete_view(request,object_id,ActionItem)

  • view
  • delete
Read More

2956 snippets posted so far.