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.
Add a Save and view next button to your admin change form.
Put the code at [link](http://www.djangosnippets.org/snippets/2006/) in a file called myapp/templates/admin/myapp/change_form.html
Note: Requires Django 1.2.
This is an example how to create a wrapping function that can read all incoming arguments, do something with them and then call the original function. This pattern works well with generic views.
Note that wrapper function accepts arguments in both ways: as a list of unnamed arguments and as a list of keyword-value pairs.
A real-world example:
def published_object_list(request, *args, **kwargs):
arg_names = object_list.func_code.co_varnames
params = dict(zip(arg_names, args))
params.update(kwargs)
params['queryset'] = params['queryset'].filter(is_published=True)
if request.is_ajax():
params['template_name'] = "ajax/" + params['template_name']
return object_list(request, **params)
Place the above code in your view, and then you can use it to specify what template to use. Set elements of the context as a dictionary and that gets passed to the template as well. For example:
In view.py:
####################################
@with_template('friends/index.html')
def friends(request, context, username):
context['user'] = User.objects.get(username = username)
in friends/index.html:
{% extends "base.html" %}
{% block content %}
<h1>{{ user.username }}'s Friends</h1>
This view snippet is a helper for implementing file download handlers. There is a standard to encode Unicode filenames properly, but many browsers have different protocols.
The default encoding is assumed to be UTF-8.
Wanted a neat way to redirect views based on GeoIP determined criteria, from HTTP_REFERER. Decorator approach seemed the best way to make it straightforward to redirect views.
To use, installed the Max Mind Python GeoIP API : http://www.maxmind.com/app/python
This is a custom widget for displaying a view only date field in the django admin.
I used it to get around this ticket:
[http://code.djangoproject.com/ticket/342](http://code.djangoproject.com/ticket/342)
This snippet shows a way to preserve GET arguments with pagination. Many people make mistakes to omit the query arguments besides page arguments for the pagination, and making sure correct may sphagettize your code.
After using Zope3/Grok for a little, I wondered how hard it would be to implement views as classes in Django, in a similar vain to how it's done in Grok. I came up with something rather simple but effective. It may be more appropriate if you use a template engine other than Django Templates, which allows you to call functions with arguments, but it's still useful none-the-less to encapsulate functions in a class.
You could, for example, extend View to be JinjaView, just replacing render_template().
A nice extension, I imagine, would be to automatically figure out the template name as well as the path prefix for it (since you probably want it to be found under packagename/templatename.html).
Inserts the output of a view, using fully qualified view name (and then some
args), a or local Django URL.
{% view view_or_url arg[ arg2] k=v [k2=v2...] %}
This might be helpful if you are trying to do 'on-server' AJAX of page
panels. Most browsers can call back to the server to get panels of content
asynchonously, whilst others (such as mobiles that don't support AJAX very
well) can have a template that embeds the output of the URL synchronously
into the main page. Yay! Go the mobile web!
Follow standard templatetag instructions for installing.
**IMPORTANT**: the calling template must receive a context variable called
'request' containing the original HttpRequest. This means you should be OK
with permissions and other session state.
**ALSO NOTE**: that middleware is not invoked on this 'inner' view.
Example usage...
Using a view name (or something that evaluates to a view name):
{% view "mymodule.views.inner" "value" %}
{% view "mymodule.views.inner" keyword="value" %}
{% view "mymodule.views.inner" arg_expr %}
{% view "mymodule.views.inner" keyword=arg_expr %}
{% view view_expr "value" %}
{% view view_expr keyword="value" %}
{% view view_expr arg_expr %}
{% view view_expr keyword=arg_expr %}
Using a URL (or something that evaluates to a URL):
{% view "/inner" %}
{% view url_expr %}
(Note that every argument will be evaluated against context except for the
names of any keyword arguments. If you're warped enough to need evaluated
keyword names, then you're probably smart enough to add this yourself!)
This example shows, how to use database views with django models. NewestArticle models contains 100 newest Articles. Remember, that NewestArticle model is read-only. Tested with mysql.
Limit rate request decorator for view.
Authenificated user can't request decorated view often then timeout.
Usage:
@limit_request_rate(time_beetween_request_sec)
def my_view(request):
...
get_cell_value from [here](http://code.activestate.com/recipes/439096/)