Using django-messages¶
To enable django-messages in your Django project make sure it is installed. You can check if django-messages was successfully installed by opening a python shell and running:
>>> import django_messages
>>>
If no error occured, you can assume that the app was installed correctly.
Edit settings¶
The next step is to add django_messages
to the INSTALLED_APPS
setting:
INSTALLED_APPS = (
...
'django_messages',
...
)
Add urls¶
To make django-messages available to your users you should include the
bunlded url-conf in your root url-conf. One example would be to edit
your main urls.py
and add a line like this:
urlpatterns = patterns(''
...
(r'^messages/', include('django_messages.urls')),
...
)
Templates¶
Django-messages provides some simple default templates which will get you
started quickly. The templates make the assumption that a base template with
the name base.html
exists which defines a block content
and a block
sidebar
. If this is not the case, or the template doesn’t fit due to other
concerns, it’s very easy to provide your own templates. Please see the
customization docs fore more details.
Templatetags and Context-Processors¶
Django-messages provides a Templatetag and a Template Context Processor to make it easy to print the number of unread messages of a user in the templates.
To use the Templatetag simply add this to your template:
{% load inbox %}
Now you can either print the number of unread messages in the users inbox by using:
{% inbox_count %}
Or you can assign the count to a variable to further process it in the template:
{% inbox_count as my_var %}
{{ my_var }}
If you want to show the inbox count on every page of your site you could also use the bundled Context Processor to add the value to every Template Context instead of loading the Templatetag. Simply add the Context Processor to the TEMPLATE_CONTEXT_PROCESSORS settings in your settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django_messages.context_processors.inbox',
)
And now every Template Context will contain a variable named
messages_inbox_count
, if the user is logged in:
{{ messages_inbox_count }}
Settings Options¶
If you do want to disable django-messages from sending either a ‘pinax-notifications’ notice or an email (fallback if ‘pinax-notifications not installed’ then set the following in your django settings:
DJANGO_MESSAGES_NOTIFY = False