When you neeed to do redirect and request object is not available, you can do it with exception.
Put exception handler somewhere request is available, for example to middleware or ModelAdmin.
Raise exception, where request is not available.
Unfortunately, it is not possible currently to use foreign keys in list filter of the admin website. list_filter=['city__country'] doesn't work.
This filter spec tries to workaround this problem.
It is also possible to have 2 filters for a foreign-key field but it requires to add a dummy field to the model. Set the fk_filterspec dictionnary on this dummy field and add 'fk':'real-field' to the dict.
Sometimes you might find useful to cache a value in different backends.
Just put this code in a file named "multicache.py" somewhere in your python path and set it in CACHE_BACKEND setting:
CACHE_BACKEND = 'multicache://?fallback=1&backends=file:///var/tmp/django_cache,locmem://'
Separate the backends settings with commas, the first one will be set as default.
Setting fallback to 1 provides fallback to default backend.
django-app-generator takes as an input your project (ORM) and generates repetitive code.
At this moment there is only one example a simple RESTservice, but goal is to build alternative
implementation of admin panel that will be fully customizable.
The project is in the beta stage, if you encounter any problems feel free to create an issue.
This filter converts slashes to spaces in a a sting and then slugify's the result. However, it ignores leading and trailing slashes. For example, it can take something like this:
/some/url/with-an-existing-slug/
And turn it into this:
some-url-with-an-existing-slug
The filter was originally written to use the *curent* url as the `disqus_identifier` for Disqus comments. For example:
{{ request.META.PATH_INFO|slug_and_slash_to_dash }}
You can use this code to sign urls for streaming distributions or change it a bit and sign normal distribution's urls.
Available settings:
CLOUDFRONT_KEY - path to private key file
CLOUDFRONT_KEY_PAIR_ID - key pair id
CLOUDFRONT_EXPIRES_IN - expiration time in seconds
CLOUDFRONT_DOMAIN - domain name
The title says it all — a subclass of FileSystemStorage which will overwrite files.
Note that saves which fail part way though will leave the original file intact (see `test_upload_fails`).
Based roughly on http://djangosnippets.org/snippets/2044/ .
if_installed checks to see if the app is in installed apps. If it is not then it excludes it from being resolved in the url structure. In this example, myapp.urls will not be imported if myapp is not installed
I have many heavy views which run slowly when accessed at same time in multiple threads. I make this decorator to allow run only one view at the time and cache returned result. Other threads will wait to complete first thread and use response from the cache if executed thread put it to cache.
Also I take idea of MintCache to refresh staled cache and return cached (stale) response while response refreshed in the cache.
Usage:
@single_cacheable(cache_timeout=60,
stale_timeout=30,
key_template='my_heavy_view-{arg1}-{arg2}')
def heavy_view(request, arg1, arg2):
response = HttpResponse()
... your code here
# cache timeout may be set from inside of view
response._cache_timeout = cache_time
return responce
The "key_template" is a template for cache key. Some my views have additinal parameter "cache_time" which set from parent page and request.path is different for these pages but they need to be cached not depending of this parameter.
Variable "key_template" in the example used named agrument, if you have no named paramaters you need to use 'my_heavy_view-{1}-{2}-{...}' where {1},{2}, {...} arguments of view.
The line with setting "key" variable may be changed to:
key = "sc_"+request.get_full_path()
if you need to take full URL path as cache key.
Ok, I just took [wolever](http://djangosnippets.org/users/wolever/)'s snippet at [http://djangosnippets.org/snippets/1926/](http://djangosnippets.org/snippets/1926/) and added template variable/filter support for range values.
For instance, you could write something like this:
`{% mkrange 1 tool_page.paginator.num_pages|add:"1" as page_range %}`
I'm just learning python and django, so any advice would be appreciated.
This is the snippet of rafacbd but upgraded... (http://djangosnippets.org/snippets/955/)
Now support keep the aspect ratio or not, changing the size string (x1, x0) this choice use pil.resize or pil.thumbnail.
Remember change the variable CAPS NAMES of the cleanup code.
This cleanup function get the original main image file name and create a regexp similar to "[filename]_[width]x[height]x[ratio].[ext]" and remove all thumbs.
By example if the main image is spain_rulez.jpg the script remove by example spain_rulez_100x100x1.jpg spain_rulez_200x150x0.jpg etc... etc...
Use this if you want to check if commenting is allowed for a certain object/model/... in a template. Like this;
`{% if not object.comments_allowed %}
<p>Comments are now closed.</p>
{% else %}
{% render_comment_form for object %}
{% endif %}`
You can replace the CommentModerator class with your own custom moderation class ofcourse.
ModelChoiceTitleField is a ModelChoiceField descendent that creates <OPTIONS> with title elements based on the field specified in title_source_field:
priority=ModelChoiceTitleField(Priority.objects.all(),
initial=Priority.objects.get(default=True).id,
title_source_field='long_description')
That will generate a `<SELECT>` element looking like:
<select name="priority" id="id_priority">
<option value="1" title="Some extremely important task.">Emergency</option>
...
</select>
In the `<option>` above, the title was retrieved from the `long_description` field for the instance of the Priority class. The word Emergency came from a call to the instance of the Priority class' `__unicode__()` method. The value of the option is the PK for the instance of the Priority class.
Deploying relocatable Django sites isn't currently as trivial as it should be (see http://code.djangoproject.com/ticket/8906, http://groups.google.com/group/django-developers/tree/browse_frm/thread/fa3661888716f940/). This snippet relocates all url patterns (similarly to http://djangosnippets.org/snippets/2129/) as well as the absolute url settings of `settings.py`.
This allows deployment under a different mount point with a single Django setting, without having to repeat the mount point again as a SCRIPT_NAME parameter supplied by the web server.
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.