Login

All snippets written in Python

Snippet List

Expand(flatten) url patterns

This is a small function for those time when you want a list of all your urls, expanding included urls, so in the end is like all your urls are in one module. This function recursively does it, so it doesnt matter how nested the includes are in the end you get one flat list.

  • urlpatterns
Read More

Function cache decorator

This is caching mechanism augmented to help address a number of common pitfalls in caching: cache stampeding, simultaneous expiry, cache invalidation, and the storing of None as a legitimate value. No doubt the automatic key generation could stand to be simplified, but it works.

  • cache
Read More

Keeping filter states after edits (Django 1.4)

When saving an edit to an object from a filtered list view you are, by default, returned to list view without any of your filters applied. This solves that problem, keeping the filtered view in a session variable until you reach a point where the session key is deleted. This solution doesn't require changes to template code...this is completely self contained within your own admin.py files. The solution presented here is a revision of a previous patch, which has been modified for Django 1.4. The Django 1.3 version exists at: http://djangosnippets.org/snippets/2531/ From chronosllc: The advantage to our solution over the above linked solution is that under different use cases the user may or may not be redirected to the filtered list_view. For example, if you edit an object and click the save and continue button, then you would lose the filter when you finally finished editing the object and clicked save. Added on here is a delete of the session key when users add objects, the reasoning we're going this route is we don't want to return users to filtered views when they just added a new object. Your mileage may vary and if so, it's easy enough to fit your own needs.

  • filter
  • admin
  • change_list
Read More

Tatsypie: additional list endpoints for custom Model's manager methods

Although configuring filtering in TastyPie is possible, it is limited to per-field filters, which are not enough for more complex filtering. If your model implement custom manager methods for complex filters, exposing these methods as TastyPie Resource list endpoints is not an easy task. The ModelResource subclass provided here does this, providing 3 ways of complex filtering: * define querysets for filters directly in the Resource declaration * use Model manager custom method * use QuerySet custom method

  • managers
  • manager
  • custom-manager
  • tastypie
Read More

Login Required Middleware with Next Parameter

Based on [onecreativenerd](http://djangosnippets.org/users/onecreativenerd/) code. Sometimes it's a real pain to use the @login_required decorator all over the views of a complicated site. This middleware requires login on every page by default and supports a list of regular expression to figure out the exceptions. This way you don't have to worry about forgetting to decorate a view. This snippet requires LOGIN_URL to be set in settings.py, and optionally allows you fill out LOGIN_EXEMPT_URLS, a tuple of regular expressions (similar to urls.py) that lists your exceptions. Example: LOGIN_EXEMPT_URLS = ( r'^about\.html$', r'^legal/', # allow the entire /legal/* subsection )

  • middleware
  • django
  • login
  • login_required
  • next
Read More

Git Revision Template Tag

This displays the short commit id from the current HEAD. Add this to your admin/base_site.html or any other template. {% block userlinks %} /&nbsp;HEAD: <span style="color:yellow">{% git_short_version %}</span> /&nbsp; {{ block.super }} {% endblock %}

  • git
Read More

Run a testcase with custom INSTALLED_APPS

This code allows you to register a model to Django that is only used for unit testing. It will not exist in the regular Django workflow. After the tests executed, the Django settings are restored. Usage: 1. Change `tests.py` into a `tests` package. 2. Place a `models.py` in the `tests` package. 3. Use the following code below to enable it. Example: class MyTest(CustomSettingsTestCase): new_settings = dict( INSTALLED_APPS=( 'django.contrib.contenttypes', 'django.contrib.auth', 'app_to_test', 'app_to_test.tests', ) ) Based on http://djangosnippets.org/snippets/1011/ as Django 1.4 version

  • settings
  • testing
  • test
  • syncdb
Read More

nbsp filter

Replaces usual spaces in string by non breaking spaces. "some words" --> "some&nbsp;words" Usage in template: {% load nbsp %} .... {{ user.full_name|nbsp }}

  • templatetag
  • nbsp
Read More

CheckboxMultiSelect with interable checkboxes

A widget for a checkbox multi select, adapted from the RadioSelect widget, which allows you to iterate over each choice in the template rather than outputting the whole thing as a ul in one go. {{ form.field.label_tag }} {% for option in form.field %} <span>{{ option }}</span> {% endfor %}

  • checkbox
  • forms
  • widget
Read More

User activation codes without additional database tables or fields

UserAuthCode generates an authentication code for Django user objects. This code can be used to verify the user's email address and to activate his account. Unlike other solutions there's no need add any tables or fields to your database. Current version is hosted on [GitHub](https://github.com/badzong/django-userauthcode). There's also an example how to use it in your Django project.

  • user
  • account
  • activation
Read More

2955 snippets posted so far.