Login

Most bookmarked snippets

Snippet List

Seamless Django and VirtualEnv integration

Automatically activates the virtualenv when running manage.py command. Just create requirements.pip file inside the root of django project and run ./manage.py update_ve in first time. Code checks requirements.pip and virtual env timestamps and require to recreate outdated environment.

  • pip
  • virtualenv
Read More

Django JSONP Decorator

This decorator function wraps a normal view function so that it can be read through a jsonp callback. Usage: @AllowJSONPCallback def my_view_function(request): return HttpResponse('this should be viewable through jsonp') It looks for a GET parameter called "callback", and if one exists, wraps the payload in a javascript function named per the value of callback. Using AllowJSONPCallback implies that the user must be logged in (and applies automatically the login_required decorator). If callback is passed and the user is logged out, "notLoggedIn" is returned instead of a normal redirect, which would be hard to interpret through jsonp. If the input does not appear to be json, wrap the input in quotes so as not to throw a javascript error upon receipt of the response.

  • jsonp
  • jsonp-decorator
Read More

Return to a filtered changelist on change form save

You've filtered your changelist in your admin site and you want to edit a few entries here; you click on an object, edit it and press save, and you end up back at the default unfiltered changelist view. This ModelAdmin override is so that if you press "save" (not "save and add another", or "save and continue editing") you end up back at your filtered changelist. There are other ways out there and other snippets to do similar; however I hadn't seen one to only redirect if you pressed save so this is what I came up with. Hopefully it's useful.

  • admin
  • redirect
Read More

iTunes Podcast RSS Feed

The syndication documentation gives a very basic beginning to creating an iTunes Podcast Feed, but leaves a lot of work left to be figured out. This is a completed example. Because I needed to obtain the duration of the podcast file programmatically (not part of this code) I limit the podcast to mp3 files. If you want to obtain durations some other way you can set the mime_type in the same manner as the other properties. This is what I'm using and should be plenty for you to customize from.

  • feed
  • rss
  • syndication
  • iTunes
  • podcast
Read More

Convenient class based views

I needed to use class based views, but I wanted to be able to use the full name of the class in my URLconf without always having to instantiate the view class before using it. What helped me was a surprisingly simple metaclass. I can now both instantiate view classes and use the instances as view functions, OR I can simply point my URLconf to my class and have the metaclass instantiate (and call) the view class for me. This works by checking the first argument to `__call__` – if it's a `HttpRequest`, it must be an actual HTTP request because it would be nonsense to attept to instantiate a view class with an `HttpRequest` instance. The `View` base class contains a overridable before method that can be used to add a common procedure to handlers of different HTTP requests. The `before` method can modify the args and kwargs of the request handlers to be able to replace, say, `model_id` with `model`.

  • views class-based-views classes metaclass
Read More

FieldStack - easy form template rendering

FieldStack simplifies forms template rendering. This is enhanced version of snippet [1786](http://djangosnippets.org/snippets/1786/)

  • template
  • fields
  • forms
  • templates
  • fieldset
  • form
  • object
  • field
  • lazy
  • fieldsets
  • groups
  • stacked
  • descriptor
Read More

Use memcached to throttle POSTs

### Problem: you want to limit posts to a view This can be accomplished with a view decorator that stores hits by IP in memcached, incrementing the cached value and returning 403's when the cached value exceeds a certain threshold for a given IP.

  • cache
  • decorator
Read More

django-admin custom filter: IS NULL/IS NOT NULL

A simple django-admin filter to allow a boolean-like filter for IS NULL/IS NOT NULL. By default it applies to CharField, IntegerField, and FileField, but you can change this by editing NullFilterSpec.fields.

  • filter
  • admin
  • null
  • filterspec
Read More

Multipart Templated Email

A replacement for sending mail that takes the name of a template which exists in both text and html formats, and uses these to create a multipart email populated with the given context.

  • template
  • email
Read More

Admin App/Model Custom Ordering

This combination of settings.py admin_reorder_tag.py and admin/base_site.html gives you the ability to define custom ordering for the apps and models in the admin app. 1. Add the setting ADMIN_REORDER to your settings.py as a tuple with each item containing a tuple of the app name and a tuple of the models within it, all defining the ordering to apply. 2. Drop the template tag code into admin_reorder_tag.py and put this into a templatetag package in one of your installed apps. 3. Drop the template code into your templates directory as admin/base_site.html

  • admin
  • app
  • ordering
Read More

Template Tag Render Decorator

I often find myself needing to create a template tag and all I'm interested in is the token and the render function. This decorator abstracts away the boilerplate code around creating the template node class which is the same each and every time.

  • template
  • tag
  • decorator
Read More

favicon Template Tag

A simple template tag that returns the favicon URL for a given arbitrary URL. Put the code into a python module in the templatetags package of a Django app (e.g. myapp/templatetags/mytags.py), and use it like this: {% load mytags %} ... <img src="{% favicon posting.url %}/> <a href="{{ posting.url }}">{{posting.title}}</a> ...

  • templatetag
  • template-tag
  • favicon
Read More

3110 snippets posted so far.