2011-09-12 06:36:16 +08:00
|
|
|
from django.conf.urls import patterns
|
2009-12-10 00:57:23 +08:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
|
|
|
from django.template import RequestContext, Template
|
2010-12-07 21:57:01 +08:00
|
|
|
from django.template.response import TemplateResponse
|
2011-03-17 16:07:40 +08:00
|
|
|
from django.views.decorators.cache import never_cache
|
2009-12-10 00:57:23 +08:00
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
TEMPLATE = """{% if messages %}
|
|
|
|
<ul class="messages">
|
|
|
|
{% for message in messages %}
|
|
|
|
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
|
|
|
|
{{ message }}
|
|
|
|
</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
{% endif %}
|
|
|
|
"""
|
2009-12-10 00:57:23 +08:00
|
|
|
|
2011-03-17 16:07:40 +08:00
|
|
|
@never_cache
|
2009-12-10 00:57:23 +08:00
|
|
|
def add(request, message_type):
|
|
|
|
# don't default to False here, because we want to test that it defaults
|
|
|
|
# to False if unspecified
|
|
|
|
fail_silently = request.POST.get('fail_silently', None)
|
|
|
|
for msg in request.POST.getlist('messages'):
|
|
|
|
if fail_silently is not None:
|
|
|
|
getattr(messages, message_type)(request, msg,
|
|
|
|
fail_silently=fail_silently)
|
|
|
|
else:
|
|
|
|
getattr(messages, message_type)(request, msg)
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2009-12-10 00:57:23 +08:00
|
|
|
show_url = reverse('django.contrib.messages.tests.urls.show')
|
|
|
|
return HttpResponseRedirect(show_url)
|
|
|
|
|
2011-03-17 16:07:40 +08:00
|
|
|
@never_cache
|
2010-12-07 21:57:01 +08:00
|
|
|
def add_template_response(request, message_type):
|
|
|
|
for msg in request.POST.getlist('messages'):
|
|
|
|
getattr(messages, message_type)(request, msg)
|
|
|
|
|
|
|
|
show_url = reverse('django.contrib.messages.tests.urls.show_template_response')
|
|
|
|
return HttpResponseRedirect(show_url)
|
2009-12-10 00:57:23 +08:00
|
|
|
|
2011-03-17 16:07:40 +08:00
|
|
|
@never_cache
|
2009-12-10 00:57:23 +08:00
|
|
|
def show(request):
|
2010-12-07 21:57:01 +08:00
|
|
|
t = Template(TEMPLATE)
|
2009-12-10 00:57:23 +08:00
|
|
|
return HttpResponse(t.render(RequestContext(request)))
|
|
|
|
|
2011-03-17 16:07:40 +08:00
|
|
|
@never_cache
|
2010-12-07 21:57:01 +08:00
|
|
|
def show_template_response(request):
|
|
|
|
return TemplateResponse(request, Template(TEMPLATE))
|
2009-12-10 00:57:23 +08:00
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
|
|
|
('^add/(debug|info|success|warning|error)/$', add),
|
|
|
|
('^show/$', show),
|
2010-12-07 21:57:01 +08:00
|
|
|
('^template_response/add/(debug|info|success|warning|error)/$', add_template_response),
|
|
|
|
('^template_response/show/$', show_template_response),
|
2009-12-10 00:57:23 +08:00
|
|
|
)
|