Login

Snippets by whiteinge

Snippet List

Model field choices as a namedtuple

This is a very flexible and concise way to [Handle choices the right way](http://www.b-list.org/weblog/2007/nov/02/handle-choices-right-way/) in model fields. * Preserves order. * Allows both a human-readable value for display in form `<select>`s as well as a code-friendly short name. * Mimic's Django's canonical [choices format](http://docs.djangoproject.com/en/1.3/ref/models/fields/#choices). * Doesn't restrict the value type. * Memory efficient. Inspired by [snippet 2373](http://djangosnippets.org/snippets/2373/) to use namedtuples as model field choices.

  • choice
  • choices
  • field
Read More

in_group template filter

Allows you to search if a user belongs to a given group. Along the same lines as snippet [390](http://www.djangosnippets.org/snippets/390/), but uses a regular ``if`` tag so it is more flexible. (Updated for efficiency. Running a boolean test on a QuerySet avoids a bit of unnecessary overhead.) (Updated to accept a list of groups.)

  • template
  • filter
  • group
Read More

Use django-admin.py instead of manage.py

A lot of people new to Django don't realize that `manage.py` is [just a wrapper](http://www.djangoproject.com/documentation/django-admin/) around the `django-admin.py` script installed with Django and isn't needed. (You may need to symlink `django-admin.py` to someplace in your system `PATH` such as `/usr/local/bin`.) The most important thing it does is to set your `PYTHONPATH` and `DJANGO_SETTINGS_MODULE` environment variables before calling `django-admin.py`. Those same settings are needed when you move your site on to a production server like Apache, so it is important to know how they work. This shell function sets those variables for you. Put it in your `.zshrc` or bash startup script. It works for both the monolithic project style and the lightweight app style of Django development [[1](http://www.pointy-stick.com/blog/2007/11/09/django-tip-developing-without-projects/)], [[2](http://www.b-list.org/weblog/2007/nov/09/projects/)]. This function isn't fancy; drop a comment if you have an improvement. Written for zsh and tested with bash 3.1.17.

  • bash
  • shell
  • zsh
Read More

Add special field lookups to the Admin list_filter display

Ever wanted to add an atypical [field lookup](http://www.djangoproject.com/documentation/db-api/#field-lookups) to the Django Admin list_filter filters, like `__isnull` or `__in`? This jQuery snippet allows you to do just that. Since you can access those additional filters by directly typing them into in the Admin URL, the tricky part is to add those to the regular list_filter display. A lot of this code is spent checking on querystring matches which is ugly and error-prone -- if you see any problems or room for improvement, drop me a comment! A suggestion of where to place this code is in `templates/admin/yourapp/yourmodel/change_list.html`. Mine kinda looks like this: {% extends "admin/change_list.html" %} {% block content %} <script src="/static/js/jquery-1.2.2.min.js" type="text/javascript"></script> <script type="text/javascript"> // the JavaScript posted in this snippet </script> {{ block.super }} {% endblock %}

  • admin
  • jquery
Read More

Admin list_display Ajax

Sometimes it can be time consuming to go through a bunch of objects in Django's Admin if you only need to update one field in each. An example of this is an `order` field that allows you to manually set the order for a queryset. **This snippet contains examples of how to set up the Admin list_display to make Ajax calls to a custom view.** The following code may not be worthy of being a snippet, it's all pretty straightforward, but here it is for those who just want to copy-and-paste.

  • admin
  • jquery
Read More

Unobtrusvie Foldable Admin Interface

Inspired by [snippet 550](http://www.djangosnippets.org/snippets/550/), this allows you to expand or collapse apps in the main Admin screen. Requires jQuery. If jquery.cookie.js is available it will remember which apps you have expanded. Recommended usage: Place the JavaScript in a file called `admin-expand.js`. Create `templates/admin/base_site.html` in your templates directory (which is also a good place to [brand your Admin](http://djangobook.com/en/1.0/chapter06/#cn70)). Put the following code near the top, and you're done (adjusting file paths as needed). {% extends "admin/base.html" %} {% block extrahead %} <script type="text/javascript" src="jquery-latest.js"></script> <script type="text/javascript" src="jquery.cookie.js"></script> <script type="text/javascript" src="admin-expand.js"></script> {% endblock %}

  • admin
  • jquery
Read More

RestfulView

In the same vein as [snippet 436](http://www.djangosnippets.org/snippets/436/), this allows you to differentiate view logic by HTTP method such as GET, POST, PUT, DELETE. This is also very useful combined with the [HttpMethodsMiddleware snippet](http://www.djangosnippets.org/snippets/174/). I am not the author, but I have found it to be very helpful.

  • rest
  • http
  • urlconf
Read More

whiteinge has posted 7 snippets.