#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 %}

{{ message.name }}

{{ message.content|safe }}


{% endfor %} Continue {% endblock %}