There are cases when rendering had already started, but you have to return Your response nevertheless. A good example is when you have a django-cms plugin and a form in it. You want to redirect after the form was processed, but normally you can't do it.
More information here:
https://github.com/divio/django-cms/issues/79
http://groups.google.com/group/django-cms/browse_thread/thread/79ab6080c80bbcb5?pli=1
When saving an edit to an object from a filtered list view you are, by default, returned to list view without any of your filters applied.
This solves that problem, keeping the filtered view in a session variable until you reach a point where the session key is deleted.
The solution presented here is hugely based off of other's work with most of the solution gained from:
[Admin: return to change_list with filter and pagination applied](http://djangosnippets.org/snippets/2415/ "Admin: return to change_list with filter and pagination applied")
This solution offered the best approach in our mind over the others listed here on snippets since the solution didn't require changes to template code...this is completely self contained within your own admin.py files.
The advantage to our solution over the above linked solution is that under different use cases the user may or may not be redirected to the filtered list_view. For example, if you edit an object and click the save and continue button, then you would lose the filter when you finally finished editing the object and clicked save.
Added on here is a delete of the session key when users add objects, the reasoning we're going this route is we don't want to return users to filtered views when they just added a new object. Your mileage may vary and if so, it's easy enough to fit your own needs.
HTHs
Use this decorator in your views to cache HttpResponse per user, so each user has his own cache, instead of a shared one as `from django.views.decorators.cache.cache_page` does.
Add this to use:
from somewhere import cache_per_user
@cache_per_user(ttl=3600, cache_post=False)
def my_view(request):
return HttpResponse("LOL %s"%(request.user))
All documentation inside the decorator are in brazilian portuguese, feel free to translate to english
A very simple decorator that caches both on-class and in memcached:
@method_cache(3600)
def some_intensive_method(self):
return # do intensive stuff`
Alternatively, if you just want to keep it per request and forgo memcaching, just do:
@method_cache()
def some_intensive_method(self):
return # do intensive stuff`
This snippet uses the admin FilterSelectMultiple widget in normal forms.
Earlier I tried this without the **internationalization javascript** and failed, so I looked around the web and found couple of posts, they worked but suggest many customizations.
I learnt from them that they had this *jsi18n* javascript included. I included it with mine and it worked.
I have written a [detailed post](http://www.rohanjain.in/coding/2011/06/20/django-using-admin-horizontal-filter-in-forms/) about this.
**Please use the updated version http://djangosnippets.org/snippets/2596/**
Unfortunately the built in Django JSON serialzer encodes GeoDjango GeometyrField as simple text. This snippet extends django's serializer and adds support for GeoJson format.
Built in JSON serializer output:
[{"pk": 1, ... "geopoint": "POINT (-76.5060419999999937 44.2337040000000030)" ... }]
GeoJSON serializer ouput:
[{"pk": 1, ... "geopoint": {"type": "Point", "coordinates": [-76.503296000000006, 44.230956999999997]}}]
This 'smart_spaceless' template tag is a replacement for Django's built-in 'spaceless'. If settings.DEBUG = True, spaces will not be removed to make debugging your template code easier. When DEBUG = False, spaces will be removed.
Happy coding!
This is a very flexible and concise way to [Handle choices the right way](http://www.b-list.org/weblog/2007/nov/02/handle-choices-right-way/) in model fields.
* Preserves order.
* Allows both a human-readable value for display in form `<select>`s as well as a code-friendly short name.
* Mimic's Django's canonical [choices format](http://docs.djangoproject.com/en/1.3/ref/models/fields/#choices).
* Doesn't restrict the value type.
* Memory efficient.
Inspired by [snippet 2373](http://djangosnippets.org/snippets/2373/) to use namedtuples as model field choices.
These base form classes add a method to return error messages as HTML encoded as JSON from Django, optionally passing in an argument to strip tags out. The method can be called in your view after checking that your form is valid. There is a ModelForm and Form class to use depending on your needs.
The sample jQuery function will take the errors returned as json, loop over the errors and insert the error after each field. If you're using a form prefix, you'll need to add a hidden field to hold the value for the prefix.
The `__all__` error(s), which are not bound to a field are appended to the end of the form, which you could easily reposition.
Happy coding!
A middleware which will protect from page hammering using flexible spanning time windows using the cache backend.
Please read the Docstring of the class for details.
**Purpose**
We often need to store x,y, width and height for a model, we can store all these values in same field by having custom field.
**How to Use**
Save this code in *customfields.py* and in your model
>*from customfields import FrameField*
>*class MyModel(models,Model)*
>* frame = FrameField()*
And in your in views, you can use as follows
>*model = MyModel.objects.get(1)*
>*print model.frame.x, model.frame.y, model.frame.width, model.frame.height*
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.