2
0

urls.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from django import forms
  2. from django.contrib import messages
  3. from django.contrib.messages.views import SuccessMessageMixin
  4. from django.http import HttpResponse, HttpResponseRedirect
  5. from django.template import engines
  6. from django.template.response import TemplateResponse
  7. from django.urls import path, re_path, reverse
  8. from django.views.decorators.cache import never_cache
  9. from django.views.generic.edit import DeleteView, FormView
  10. from .models import SomeObject
  11. TEMPLATE = """{% if messages %}
  12. <ul class="messages">
  13. {% for message in messages %}
  14. <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
  15. {{ message }}
  16. </li>
  17. {% endfor %}
  18. </ul>
  19. {% endif %}
  20. """
  21. @never_cache
  22. def add(request, message_type):
  23. # Don't default to False here to test that it defaults to False if
  24. # unspecified.
  25. fail_silently = request.POST.get('fail_silently', None)
  26. for msg in request.POST.getlist('messages'):
  27. if fail_silently is not None:
  28. getattr(messages, message_type)(request, msg, fail_silently=fail_silently)
  29. else:
  30. getattr(messages, message_type)(request, msg)
  31. return HttpResponseRedirect(reverse('show_message'))
  32. @never_cache
  33. def add_template_response(request, message_type):
  34. for msg in request.POST.getlist('messages'):
  35. getattr(messages, message_type)(request, msg)
  36. return HttpResponseRedirect(reverse('show_template_response'))
  37. @never_cache
  38. def show(request):
  39. template = engines['django'].from_string(TEMPLATE)
  40. return HttpResponse(template.render(request=request))
  41. @never_cache
  42. def show_template_response(request):
  43. template = engines['django'].from_string(TEMPLATE)
  44. return TemplateResponse(request, template)
  45. class ContactForm(forms.Form):
  46. name = forms.CharField(required=True)
  47. slug = forms.SlugField(required=True)
  48. class ContactFormViewWithMsg(SuccessMessageMixin, FormView):
  49. form_class = ContactForm
  50. success_url = show
  51. success_message = "%(name)s was created successfully"
  52. class DeleteFormViewWithMsg(SuccessMessageMixin, DeleteView):
  53. model = SomeObject
  54. success_url = '/show/'
  55. success_message = 'Object was deleted successfully'
  56. urlpatterns = [
  57. re_path('^add/(debug|info|success|warning|error)/$', add, name='add_message'),
  58. path('add/msg/', ContactFormViewWithMsg.as_view(), name='add_success_msg'),
  59. path('delete/msg/<int:pk>', DeleteFormViewWithMsg.as_view(), name='success_msg_on_delete'),
  60. path('show/', show, name='show_message'),
  61. re_path(
  62. '^template_response/add/(debug|info|success|warning|error)/$',
  63. add_template_response, name='add_template_response',
  64. ),
  65. path('template_response/show/', show_template_response, name='show_template_response'),
  66. ]