summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorLoic Bistuer <loic.bistuer@gmail.com>2014-07-29 18:45:16 +0700
committerTim Graham <timograham@gmail.com>2014-07-31 13:43:46 -0400
commit75790808993691d2869db577df6db47ecbef6f5a (patch)
treee2b103c612d5cfc22b1d498016c34841600c50a7 /docs
parent76f2f58a1853fd7a774f3df3fe454022a0206ff9 (diff)
Used JsonResponse in CBV examples.
Thanks Hiroki Kiyohara and Tim Graham for the reviews.
Diffstat (limited to 'docs')
-rw-r--r--docs/topics/class-based-views/generic-editing.txt13
-rw-r--r--docs/topics/class-based-views/mixins.txt16
2 files changed, 11 insertions, 18 deletions
diff --git a/docs/topics/class-based-views/generic-editing.txt b/docs/topics/class-based-views/generic-editing.txt
index 059342afd8..f09372c7ff 100644
--- a/docs/topics/class-based-views/generic-editing.txt
+++ b/docs/topics/class-based-views/generic-editing.txt
@@ -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
diff --git a/docs/topics/class-based-views/mixins.txt b/docs/topics/class-based-views/mixins.txt
index 40ba615088..6c6200cd84 100644
--- a/docs/topics/class-based-views/mixins.txt
+++ b/docs/topics/class-based-views/mixins.txt
@@ -608,8 +608,7 @@ conversion to JSON once.
For example, a simple JSON mixin might look something like this::
- import json
- from django.http import HttpResponse
+ from django.http import JsonResponse
class JSONResponseMixin(object):
"""
@@ -619,19 +618,20 @@ For example, a simple JSON mixin might look something like this::
"""
Returns a JSON response, transforming 'context' to make the payload.
"""
- return HttpResponse(
- self.convert_context_to_json(context),
- content_type='application/json',
+ return JsonResponse(
+ self.get_data(context),
**response_kwargs
)
- def convert_context_to_json(self, context):
- "Convert the context dictionary into a JSON object"
+ def get_data(self, context):
+ """
+ Returns an object that will be serialized as JSON by json.dumps().
+ """
# Note: This is *EXTREMELY* naive; in reality, you'll need
# to do much more complex handling to ensure that arbitrary
# objects -- such as Django model instances or querysets
# -- can be serialized as JSON.
- return json.dumps(context)
+ return context
.. note::