Efficient and lightweight Django notices
License
uggedal/django-notices
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
django-notices ============== Provides support for sending small notices to the next redirected page. One normally does this after having handled a successful POST request. To make the API transparent the original HttpResponseRedirect was subclassed with a class that supports sending notices. Usage ----- Just use HttpResponseRedirectWithNotice with a keyword argument and your notice: from notices import HttpResponseRedirectWithNotice from django.shortcuts import render_to_response def add_counter(req, number): if number > 0: Counter.objects.create(number=number) notice = "Created counter with %s" % number return HttpResponseRedirectWithNotice("/", success=notice) else: return render_to_response('form.html', { 'error_message': "Counters have to be positive" }) There is also a shortcut similar to Django's for easily handeling redirects with views, objects, or urls: from notices.shortcuts import redirect from django.shortcuts import render_to_response def add_counter(req, number): if number > 0: Counter.objects.create(number=number) notice = "Created counter with %s" % number return redirect("/", success=notice) else: return render_to_response('form.html', { 'error_message': "Counters have to be positive" }) To handle these messages in a template one could do something like this: {% if notices %} <ul class="notices"> {% for type, notice in notices.items %} <li class="{{ type }}">{{ notice }}</li> {% endfor %} </ul> {% endif %} By default HttpResponseRedirectWithNotice supports notices with the type of success, error, and notice. This can be changed in settings.py: NOTICE_TYPES = ("success", "failure") Installation ------------ To install django-notices one have to: 1. add the notices directory to the Python path 2. add the notices and the auth_without_messages template context processors to settings.py and disable the default auth context processor (note that this currently breaks the admin in DEBUG mode): TEMPLATE_CONTEXT_PROCESSORS = ( # These are the default Django processors. # Disable the auth core context processor.. #'django.core.context_processors.auth', 'django.core.context_processors.debug', 'django.core.context_processors.i18n', 'django.core.context_processors.media', 'django.core.context_processors.request', 'notices.context_processors.notices', 'notices.context_processors.auth_without_messages', ) Rationale --------- An alternative to django-notices are the bundled messages system in django.contrib.auth. My main grief with this solution is that it only supports on type of message to be sent. Another problem is that is uses the database, which results in one extra query for each page view. There are cookie based solutions inspired by Rails available for Django, but I don't like using cookies to handle such state. This solution is inspired by merb-core and uses a query parameter containing the message. The message is base64 and urlencoded in addition to being signed with your project's SECRET_KEY in settings.py.
About
Efficient and lightweight Django notices
Resources
License
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published