diff options
author | luxagraf <sng@luxagraf.net> | 2018-12-29 08:37:39 -0600 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2018-12-29 08:37:39 -0600 |
commit | 4f7b84194b056b5d6d9acca4cceb2cabc04fd8a5 (patch) | |
tree | 7fe109e7aeaddab7aa5e7f46f99414064a248e52 /apps/utils/views.py | |
parent | 02f520038e3c6d5a01c9545e9b1c3eb91e4e016c (diff) |
cleaned up JS and made modal handler.
Diffstat (limited to 'apps/utils/views.py')
-rw-r--r-- | apps/utils/views.py | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/apps/utils/views.py b/apps/utils/views.py index 595e102..ec3a902 100644 --- a/apps/utils/views.py +++ b/apps/utils/views.py @@ -1,6 +1,6 @@ from itertools import chain import json -from django.http import Http404, HttpResponse +from django.http import Http404, HttpResponse, JsonResponse from django.apps import apps from django.views.generic import ListView from django.views.generic.base import View, RedirectView @@ -42,6 +42,31 @@ class LoggedInViewWithUser(View): return kwargs + +class AjaxableResponseMixin: + """ + Mixin to add AJAX support to a form. + Must be used with an object-based FormView (e.g. CreateView) + """ + def form_invalid(self, form): + response = super().form_invalid(form) + if self.request.is_ajax(): + return JsonResponse(form.errors, status=400) + else: + return response + + def form_valid(self, form): + # We make sure to call the parent's form_valid() method because + # it might do some processing (in the case of CreateView, it will + # call form.save() for example). + response = super().form_valid(form) + if self.request.is_ajax(): + data = { + 'pk': self.object.pk, + } + return JsonResponse(data) + else: + return response ''' class TagAutocomplete(autocomplete.Select2QuerySetView): def get_queryset(self): |