Snippet List
Middleware class that checks the user agent against a known list of strings found in mobile devices, and if matched it then tries to determine the name of the template being rendered based on the convention of having every view use a keyword arg called "template". It then adds "mobile" to the template name and if the mobile template exists, it will override the "template" arg for the view with the mobile template name.
- middleware
- template
- mobile
There's a whole range of examples online for resizing images in Django some of which are incredibly comprehensive with a wide variety of options. Here's my take on the task that serves as a simple drop in for when you don't want to include a separate app.
- Only generates the resized image when first requested.
- Handles maintaining proportions when specifying only a width or a height.
- Makes use of PIL.ImageOps.fit for cropping without reinventing the wheel.
- template
- tag
- image
- pil
- thumbnail
- resize
This combination of settings.py admin_reorder_tag.py and admin/base_site.html gives you the ability to define custom ordering for the apps and models in the admin app.
1. Add the setting ADMIN_REORDER to your settings.py as a tuple with each item containing a tuple of the app name and a tuple of the models within it, all defining the ordering to apply.
2. Drop the template tag code into admin_reorder_tag.py and put this into a templatetag package in one of your installed apps.
3. Drop the template code into your templates directory as admin/base_site.html
Template designers often require access to individual form fields or sets of form fields to control complex form layouts. While this is possible via looping through form fields in the template it can lead to ugly logic inside templates as well as losing the ability to use the as_* form rendering methods.
This class when mixed into a form class provides hooks for template designers to access individual fields or sets of fields based on various criteria such as field name prefix or fields appearing before or after certain fields in the form, while still being able to use the as_* form rendering methods against these fields.
stephen_mcd has posted 7 snippets.