Snippet List
This small app can display messages to users after they login and before they get to the normal landing page. This can be useful for displaying maintenance notices, information on new features, or a one-day-sale on shoes.
To redirect to the MOTD view after login, change:
`<input type="hidden" name="next" value="{{ next }}" />`
to:
`<input type="hidden" name="next" value="{% url django_motd.views.motd %}?next={{ next }}" />`
in your login.html template.
This is the code for a template tag. Put this code in your template to render your messages:
{% for message in messages %}
{% render_user_message message %}
{% endfor %}
When you're adding a message to the user's message set, follow these rules: If you want a message to appear as an error, append "0001" to the end of it. To appear as a notice, append "0002" to it. To appear as a happy message, appear "0000" to it. If no code is present, it will default to displaying as an error. This makes use of the classes "error", "notice", and "success", so you need to define these in your CSS.
For help with custom template tags, see [the Django docs](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/)
This exception is util when you want to raise an exception but want its message be shown as a message to the user, with no error 500 or 404 pages.
To use it, just append the middleware in the MIDDLEWARE_CLASSES setting and raises HttpMessage when necessary.
- http
- redirect
- message
- exception
Template:
`<div id="messageBox_{{ forloop.counter0 }}" style="border:1px solid #ccc;background-color:white" onclick="document.getElementById ('messageBox_{{ forloop.counter0 }}').style.display = 'none';">
{% ifstartswith message "#ok#" %}
<font color="green">
{% endifstartswith %}
{% ifstartswith message "#error#" %}
<font color="red">
{% endifstartswith %}
{{ message|cut:"#ok#"|cut:"#error#" }}
</font>
</div>`
In a view you can now do something like that:
` request.user.message_set.create(message="#ok#Hello User, this is a ok message!")`
- template
- templatetags
- message
Django EmailMessage class has no cc support and has bug in bcc support.
Core developers won't add cc support (see ticket http://code.djangoproject.com/ticket/5790),
and I don't want to wait half a year until they will realize they have a flaw that bcc recipients are sent to regular "to:" recipients and fix it.
So, if you want to use EmailMessage class right now, you'd better use FixedEmailMessage class. Class contract is the same, except for a new cc constructor argument.
- email
- smtp
- mail
- message
- emailmessage
- cc
Flash message add-on for Django. Uses sessions. Behavior is such that you set a flash message in a view. That message is stored in the sesssion. Then whenever it is that the message gets displayed, it is removed from the session (never to be heard from again)
**Installation:**
In your settings, enable the following items.
TEMPLATE_CONTEXT_PROCESSORS
django.core.context_processors.request
MIDDLEWARE_CLASSES
django.contrib.sessions.middleware.SessionMiddleware
Then put it into a file called flash.py in your templatetags directory.
**Usage:**
It's pretty simple. Do something like this in your view ..
>>>request.session['flash_msg'] = 'Your changes have been save'
>>>request.session['flash_params'] = {'type': 'success'}
And maybe put something like this in your template
{% load flash %}
{% flash %}
<h2>{{ params.type }}</h2>
{{ msg }}
{% endflash %}
It also support a flash template, you can specify a file
FLASH_TEMPLATE in your settings file and then that file will be rendered with msg and params as available variable.
Usage for this would simply be `{% flash_template %}` and then you gotta make a template file that does whatever you like.
Outside of that just be aware you need the Django session middleware and request context installed in your app to use this.
6 snippets posted so far.