Last active
October 2, 2017 17:04
-
-
Save darwing1210/9038543d477499c3a44ef7b16d0d5cde to your computer and use it in GitHub Desktop.
Some useful Django ajax views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JSONResponseMixin(object): | |
''' | |
This is a slightly modified version from django-braces project | |
(https://github.com/brack3t/django-braces) | |
''' | |
content_type = None | |
json_dumps_kwargs = None | |
def get_content_type(self): # pragma: no cover | |
return self.content_type or "application/json" | |
def get_json_dumps_kwargs(self): | |
if self.json_dumps_kwargs is None: | |
self.json_dumps_kwargs = {} | |
self.json_dumps_kwargs.setdefault('ensure_ascii', False) | |
return self.json_dumps_kwargs | |
def render_json_response(self, context_dict, status=200): # NOQA pragma: no cover | |
""" | |
Limited serialization for shipping plain data. Do not use for models | |
or other complex or custom objects. | |
""" | |
json_context = json.dumps( | |
context_dict, | |
cls=DjangoJSONEncoder, | |
**self.get_json_dumps_kwargs() | |
).encode('utf-8') | |
return HttpResponse( | |
json_context, | |
content_type=self.get_content_type(), | |
status=status | |
) | |
class AjaxCreateView(JSONResponseMixin, CreateView): | |
template_name = None | |
model = None | |
form_class = None | |
def pre_save(self): | |
pass | |
def post_save(self): | |
pass | |
def form_valid(self, form): | |
""" | |
If the request is ajax, save the form and return a json response. | |
Otherwise return super as expected. | |
""" | |
self.object = form.save(commit=False) | |
self.pre_save() | |
self.object.save() | |
if hasattr(form, 'save_m2m'): | |
form.save_m2m() | |
self.post_save() | |
if self.request.is_ajax(): | |
return self.render_json_response({'status': 'ok', 'message': ''}) | |
return HttpResponseRedirect(self.get_success_url()) | |
def form_invalid(self, form): | |
""" | |
We have errors in the form. If ajax, return them as json. | |
Otherwise, proceed as normal. | |
""" | |
if self.request.is_ajax(): | |
return self.render_json_response({'status': 'error', 'message': form.errors}) # NOQA | |
return super(AjaxCreateView, self).form_invalid(form) | |
def render_to_response(self, context, **response_kwargs): | |
if not self.template_name: | |
return HttpResponse(status=200) | |
else: | |
response_kwargs.setdefault('content_type', self.content_type) | |
return self.response_class( | |
request=self.request, | |
template=self.get_template_names(), | |
context=context, | |
using=self.template_engine, | |
**response_kwargs | |
) | |
class AjaxDeleteView(JSONResponseMixin, DeleteView): | |
model = None | |
template_name = 'fake_delete.html' | |
def pre_delete(self): # pragma: no cover | |
pass | |
def post_delete(self): # pragma: no cover | |
pass | |
def get_success_result(self): # pragma: no cover | |
return {'status': 'ok'} | |
def delete(self, request, *args, **kwargs): | |
""" | |
The same logic as in DeleteView but some hooks and | |
JSON response in case of AJAX request | |
""" | |
self.object = self.get_object() | |
self.pre_delete() | |
self.object.delete() | |
self.post_delete() | |
if self.request.is_ajax(): | |
return self.render_json_response(self.get_success_result()) | |
success_url = self.get_success_url() | |
return HttpResponseRedirect(success_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment