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.
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.
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
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
)
This displays the short commit id from the current HEAD.
Add this to your admin/base_site.html or any other template.
{% block userlinks %}
/ HEAD: <span style="color:yellow">{% git_short_version %}</span> /
{{ block.super }}
{% endblock %}
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
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 %}
Html code for ["Google v3 geocoding for Geodjango admin site"](http://djangosnippets.org/snippets/2839/)
Rename the snippet as gmgdav3.html and save it to template/admin
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.