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).
A relatively simple Photo model which generates its own thumbnail when saved. A default size is specified as `thumb_size` in the `save()` arguments.
Other thumbnailing strategies don't save the thumbnail dimensions, and since the actual dimensions of the thumbnail created by PIL's `thumbnail` method are somewhat non-deterministic, it is difficult to create an `img` tag with proper height and width attributes. This approach makes that task simple.
This approach works well if only one thumbnail size is required. It could easily be adapted to support two or three thumbnail sizes, but adding more sizes would quickly get unwieldy.
This was adapted from http://biohackers.net/wiki/Django1.0/Thumbnail
This snippet makes Django templates support `break` and `continue` in loops. It is actually more powerful than the respective Python statements as it allows breaking and continuing from an outer loop, not just the innermost.
`break` and `continue` are implemented as template filters, with the input value being the loop variable. For example, to break from the current `for` loop use `forloop|break`, and to continue from the next outer loop use `forloop.parentloop|continue`.
The implementation monkeypatches Django (specifically Nodelist and ForNode) and has been tested on v1.2 with Python 2.6.
A common problem (it hit the Django mailinglist a couple of times) is that if you get `models.Topping.objects.all()`, you get a list of toppings, although they stand for other classes such as `SalamiTopping` or `CheeseTopping`. If you need the actual object, just derive `Topping` from `PolymorphicModel`, and say `topping.actual_instance`. This will give you e.g. a `SalamiTopping`.
Sometimes you just want to check for the actual class. You can get it by saying `topping.content_type.model_class()`.
There is a slight performance impact when creating objects because they have to be saved twice.
NEWS: A good alternative to this approach is the [InheritanceManager](https://github.com/carljm/django-model-utils/blob/master/README.rst).