2015-01-28 20:35:27 +08:00
|
|
|
from django import forms
|
2009-12-10 00:57:23 +08:00
|
|
|
from django.contrib import messages
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2015-01-10 05:59:00 +08:00
|
|
|
from django.template import engines
|
2010-12-07 21:57:01 +08:00
|
|
|
from django.template.response import TemplateResponse
|
2018-12-08 06:52:28 +08:00
|
|
|
from django.urls import path, re_path, reverse
|
2011-03-17 16:07:40 +08:00
|
|
|
from django.views.decorators.cache import never_cache
|
2013-03-19 06:48:47 +08:00
|
|
|
from django.views.generic.edit import FormView
|
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
|
|
|
|
2013-11-03 04:12:09 +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):
|
2017-01-05 22:23:39 +08:00
|
|
|
# Don't default to False here to test that it defaults to False if
|
|
|
|
# unspecified.
|
2009-12-10 00:57:23 +08:00
|
|
|
fail_silently = request.POST.get('fail_silently', None)
|
|
|
|
for msg in request.POST.getlist('messages'):
|
|
|
|
if fail_silently is not None:
|
2017-01-05 22:23:39 +08:00
|
|
|
getattr(messages, message_type)(request, msg, fail_silently=fail_silently)
|
2009-12-10 00:57:23 +08:00
|
|
|
else:
|
|
|
|
getattr(messages, message_type)(request, msg)
|
2017-01-05 22:23:39 +08:00
|
|
|
return HttpResponseRedirect(reverse('show_message'))
|
2009-12-10 00:57:23 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
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)
|
2017-01-05 22:23:39 +08:00
|
|
|
return HttpResponseRedirect(reverse('show_template_response'))
|
2009-12-10 00:57:23 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2011-03-17 16:07:40 +08:00
|
|
|
@never_cache
|
2009-12-10 00:57:23 +08:00
|
|
|
def show(request):
|
2015-01-10 05:59:00 +08:00
|
|
|
template = engines['django'].from_string(TEMPLATE)
|
|
|
|
return HttpResponse(template.render(request=request))
|
2009-12-10 00:57:23 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2011-03-17 16:07:40 +08:00
|
|
|
@never_cache
|
2010-12-07 21:57:01 +08:00
|
|
|
def show_template_response(request):
|
2015-01-10 05:59:00 +08:00
|
|
|
template = engines['django'].from_string(TEMPLATE)
|
|
|
|
return TemplateResponse(request, template)
|
2009-12-10 00:57:23 +08:00
|
|
|
|
2013-03-19 06:48:47 +08:00
|
|
|
|
|
|
|
class ContactForm(forms.Form):
|
|
|
|
name = forms.CharField(required=True)
|
|
|
|
slug = forms.SlugField(required=True)
|
|
|
|
|
|
|
|
|
|
|
|
class ContactFormViewWithMsg(SuccessMessageMixin, FormView):
|
|
|
|
form_class = ContactForm
|
|
|
|
success_url = show
|
|
|
|
success_message = "%(name)s was created successfully"
|
|
|
|
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2018-12-08 06:52:28 +08:00
|
|
|
re_path('^add/(debug|info|success|warning|error)/$', add, name='add_message'),
|
|
|
|
path('add/msg/', ContactFormViewWithMsg.as_view(), name='add_success_msg'),
|
|
|
|
path('show/', show, name='show_message'),
|
|
|
|
re_path(
|
|
|
|
'^template_response/add/(debug|info|success|warning|error)/$',
|
|
|
|
add_template_response, name='add_template_response',
|
|
|
|
),
|
|
|
|
path('template_response/show/', show_template_response, name='show_template_response'),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|