Login

Temporary admin messages (MOTD)

Author:
bthomas
Posted:
February 5, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#models.py
from datetime import datetime
from django.db import models
from django.utils.translation import gettext_lazy as _

class MotdMessageManager(models.Manager):
    def active_messages(self):
        dtnow = datetime.utcnow()
        return super(MotdMessageManager, self).get_query_set().filter(
            enabled=True, start_time__lte=dtnow, end_time__gte=dtnow)

class MotdMessage(models.Model):
    enabled = models.BooleanField(default=True)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    name = models.CharField(_('name'), unique=True, max_length=100)
    content = models.TextField(_('content'), blank=True)
    
    objects = MotdMessageManager()
    
    class Meta:
        ordering = ['start_time']
    
    def __unicode__(self):
        return self.name


#views.py
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from models import MotdMessage

def motd(request, template_name='motd/messages.html', redirect_field_name=REDIRECT_FIELD_NAME):
    redirect_to = request.REQUEST.get(redirect_field_name, '')
    if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
        redirect_to = settings.LOGIN_REDIRECT_URL
    messages = MotdMessage.objects.active_messages()
    if not messages:
        return HttpResponseRedirect(redirect_to)
    return render_to_response(template_name, {redirect_field_name: redirect_to,
                'messages': messages}, context_instance=RequestContext(request))

#admin.py
from django.contrib import admin
import models

admin.site.register(models.MotdMessage)

#templates/motd/messages.html
{% extends "base.html" %}

{% block content %}

{% for message in messages %}
<h2>{{ message.name }}</h2>
<p>{{ message.content|safe }}</p>
<br/>
{% endfor %}

<a href="{{ next }}">Continue</a>

{% endblock %}

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

jamesgpearce (on June 25, 2009):

Useful, but a bit tricky to wireup:

1) mention you need to add it to your urls.py

2) the {% url %} parameter needs to match your own module name

3) the messages.html template needs to be created (or just use that instead of base.html)

4) 'messages' is already used in admin context for something else (the update alerts), so you need to change that to a different name

Works OK in the end, though.

#

Please login first before commenting.