1. Base your test case off `ModuleTestCase` and set a class attribute containing a dictionary of modules which you want to be able to revert the values of.
2. Use `self.modulename.attribute = something` in your `setUp` method or test cases to change the module's attribute values.
3. The values will be automatically restored when each test case finishes.
For the common case of reverting the settings module, just use the `SettingsTestCase` as your base class.
A Widget for displaying ManyToMany ids in the "raw_id" interface rather than in a <select multiple> box. Display user-friendly value like the ForeignKeyRawId widget
Quick and simple twitterize filter to turn Twitter usernames into profile links on your own sites.
Add the filter code to your own template tags (http://docs.djangoproject.com/en/dev/howto/custom-template-tags/).
This is custom tag I wrote for myself for solving situations when you have filter form and page
numbers in the same page. You want to change ?page=.. or add it if it doesn't exist to save
filter form data while moving through pages.
Usage: place this code in your application_dir/templatetags/add_url_parameter.py
In template:
{% load add_get_parameter %}
<a href="{% add_get_paramater param1='const_value',param2=variable_in_context %}">
Link with modified params
</a>
It's required that you have 'django.core.context_processors.request' in TEMPLATE_CONTEXT_PROCESSORS
URL: [http://django.mar.lt/2010/07/add-get-parameter-tag.html](http://django.mar.lt/2010/07/add-get-parameter-tag.html)
Lets you include another template and pass in named variables. Use like:
{% partial field_template field=form.city %}
If no key is specified, it will use "item" instead. You may pass in multiple variables as comma-separated key=value pairs.
Temporary and permanent view redirect decorators. Simplifies views that always redirect to a specific or configured url. You can use the reverse() function (or any other runtime-required lookup) via `lambda` to define the redirect url.
Django JSON view decorator. Dumps the object returned from the view function. Allows you to customize the JSON dumps() function using the available keyword arguments available from the simplejson module. By default, indents the output with 4 characters.
This middleware makes the page load faster because it moves all tags <script> to the end of document.
When a tag <script> loads, it blocks parallel loading, so, the best practice is load them after load CSS and images.
To use it, just put in a file and add to your setting MIDDLEWARE_CLASSES.
Use it like below:
totals = MyClass.aggregate(
is_enabled_yes=CountCase('is_enabled', when=True),
is_enabled_no=CountCase('is_enabled', when=False),
count_if=CountCase('id', case="another_field in ('a','b')", when=True),
)
Just use it like below:
from downloaded_file import SumCase
MyClass.objects.aggregate(
sum1=SumCase('salary', case='salary < 4', when=True),
sum1=SumCase('salary', case='type', when='director'),
)
This snippet applies the improved pickledobject snippet http://djangosnippets.org/snippets/1694/ to django-techblog's "fields.py" file. Necessary for using postgresql/psycopg2.
The admin site uses a CSS class for each kind of field so that they can be styled easily. This snippet gives ModelForms a similar feature.
See also: [James Bennett's explanation of formfield_callback](http://stackoverflow.com/questions/660929/how-do-you-change-the-default-widget-for-all-django-date-fields-in-a-modelform/661171#661171)
This middleware will prevent access to the admin if the users IP isn't in the INTERNAL_IPS setting, by comparing the request path with the reversed index URL of the default admin site, ultimately raising a 404 (unless DEBUG = True).