Login

All snippets

Snippet List

Serve static media files from app/media subdirectory

This view will serve media files from all media subdirectories of apps in your INSTALLED_APPS setting. Save the view as media.py in your django site folder and add to urls.py: if settings.DEBUG: urlpatterns += patterns('', (r'^media/(?P<appname>\w+)/(?P<path>.+)$', 'devel_site.media.serve_apps') )` Now suppose your installed apps setting looks like: INSTALLED_APPS = ('org.myself.myapp', ...) Then a request to http://localhost/media/myapp/directory/file.css will serve the file org/myself/myapp/media/directory/file.css.

  • media
  • static-media
Read More

Render specific blocks from templates (useful for AJAX, alternative)

Special thanks to the author of snippet 769 who provided most of the code for this snippet. Major differences: 1.Simpler/better handling of "extends" block tag 2.Searches If/Else blocks 3.Less code 4.Allow list of templates to be passed which is closer to the behavior of render_to_response

  • template
  • block
  • templates
  • render
  • context
  • blocks
Read More

very simplistic url cache flushing

This is an (overly simple) example of how to manually delete something from the cache. Usage is simply `delete_url_cache('/some_view/')` This most likely doesn't work when using Vary headers but does seem to work fine for the most general case of simply needing to programmatically flush the cache after certain actions.

  • cache
Read More
Author: jpt
  • 1
  • 3

Replace Paragraph Tags for Flash

This template tag does two things needed to display content from something like a TextField wrapped with TinyMCE in Flash's htmlText component: 1. Replace `<p>` tags with `<br />` 2. Strip all occurances of `\n`

  • template
  • tag
Read More

sorl.thumbnail processor: white background

If you want to resize transparent PNGs with the `{% thumbnail %}` templatetag, they'll sometimes get an ugly black background that looks even more ugly on a white background. This processor puts the image on a white background. You can simply change the background color by replacing `white` with any other color. To use this filter simple put the following two lines of code in your settings file: from sorl.thumbnail.defaults import PROCESSORS as THUMBNAIL_PROCESSORS THUMBNAIL_PROCESSORS = ('path.to.white_background',) + THUMBNAIL_PROCESSORS

  • png
  • background
  • sorl.thumbnail
Read More

Readonly admin fields

Put this code and import it where you define your ModelAdmin-classes. # typical admin.py file: from django.contrib import admin from foo.bar import ReadOnlyAdminFields class MyModelAdmin(ReadOnlyAdminFields, admin.ModelAdmin): readonly = ('field1', 'field2',)

  • django
  • admin
  • field
  • readonly
Read More

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

3109 snippets posted so far.