|
@@ -217,9 +217,7 @@ AJAX example
|
|
|
Here is a simple example showing how you might go about implementing a form that
|
|
|
works for AJAX requests as well as 'normal' form POSTs::
|
|
|
|
|
|
- import json
|
|
|
-
|
|
|
- from django.http import HttpResponse
|
|
|
+ from django.http import JsonResponse
|
|
|
from django.views.generic.edit import CreateView
|
|
|
from myapp.models import Author
|
|
|
|
|
@@ -228,15 +226,10 @@ works for AJAX requests as well as 'normal' form POSTs::
|
|
|
Mixin to add AJAX support to a form.
|
|
|
Must be used with an object-based FormView (e.g. CreateView)
|
|
|
"""
|
|
|
- def render_to_json_response(self, context, **response_kwargs):
|
|
|
- data = json.dumps(context)
|
|
|
- response_kwargs['content_type'] = 'application/json'
|
|
|
- return HttpResponse(data, **response_kwargs)
|
|
|
-
|
|
|
def form_invalid(self, form):
|
|
|
response = super(AjaxableResponseMixin, self).form_invalid(form)
|
|
|
if self.request.is_ajax():
|
|
|
- return self.render_to_json_response(form.errors, status=400)
|
|
|
+ return JsonResponse(form.errors, status=400)
|
|
|
else:
|
|
|
return response
|
|
|
|
|
@@ -249,7 +242,7 @@ works for AJAX requests as well as 'normal' form POSTs::
|
|
|
data = {
|
|
|
'pk': self.object.pk,
|
|
|
}
|
|
|
- return self.render_to_json_response(data)
|
|
|
+ return JsonResponse(data)
|
|
|
else:
|
|
|
return response
|
|
|
|