Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
d0bd5330
Kaydet (Commit)
d0bd5330
authored
Agu 12, 2015
tarafından
Sambhav Satija
Kaydeden (comit)
Tim Graham
Agu 12, 2015
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #25254 -- Added JsonResponse json_dumps_params parameter.
üst
290145e6
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
22 additions
and
3 deletions
+22
-3
response.py
django/http/response.py
+6
-2
request-response.txt
docs/ref/request-response.txt
+8
-1
1.9.txt
docs/releases/1.9.txt
+4
-0
tests.py
tests/httpwrappers/tests.py
+4
-0
No files found.
django/http/response.py
Dosyayı görüntüle @
d0bd5330
...
...
@@ -492,12 +492,16 @@ class JsonResponse(HttpResponse):
``django.core.serializers.json.DjangoJSONEncoder``.
:param safe: Controls if only ``dict`` objects may be serialized. Defaults
to ``True``.
:param json_dumps_params: A dictionary of kwargs passed to json.dumps().
"""
def
__init__
(
self
,
data
,
encoder
=
DjangoJSONEncoder
,
safe
=
True
,
**
kwargs
):
def
__init__
(
self
,
data
,
encoder
=
DjangoJSONEncoder
,
safe
=
True
,
json_dumps_params
=
None
,
**
kwargs
):
if
safe
and
not
isinstance
(
data
,
dict
):
raise
TypeError
(
'In order to allow non-dict objects to be '
'serialized set the safe parameter to False'
)
if
json_dumps_params
is
None
:
json_dumps_params
=
{}
kwargs
.
setdefault
(
'content_type'
,
'application/json'
)
data
=
json
.
dumps
(
data
,
cls
=
encoder
)
data
=
json
.
dumps
(
data
,
cls
=
encoder
,
**
json_dumps_params
)
super
(
JsonResponse
,
self
)
.
__init__
(
content
=
data
,
**
kwargs
)
docs/ref/request-response.txt
Dosyayı görüntüle @
d0bd5330
...
...
@@ -912,7 +912,7 @@ types of HTTP responses. Like ``HttpResponse``, these subclasses live in
JsonResponse objects
====================
.. class:: JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, **kwargs)
.. class:: JsonResponse(data, encoder=DjangoJSONEncoder, safe=True,
json_dumps_params=None,
**kwargs)
An :class:`HttpResponse` subclass that helps to create a JSON-encoded
response. It inherits most behavior from its superclass with a couple
...
...
@@ -934,6 +934,13 @@ JsonResponse objects
``dict`` instances are allowed). If ``safe`` is ``True`` and a non-``dict``
object is passed as the first argument, a :exc:`TypeError` will be raised.
The ``json_dumps_params`` parameter is a dictionary of keyword arguments
to pass to the ``json.dumps()`` call used to generate the response.
.. versionchanged:: 1.9
The ``json_dumps_params`` argument was added.
Usage
-----
...
...
docs/releases/1.9.txt
Dosyayı görüntüle @
d0bd5330
...
...
@@ -545,6 +545,10 @@ Requests and Responses
* Added :meth:`HttpRequest.get_port() <django.http.HttpRequest.get_port>` to
fetch the originating port of the request.
* Added the ``json_dumps_params`` parameter to
:class:`~django.http.JsonResponse` to allow passing keyword arguments to the
``json.dumps()`` call used to generate the response.
Tests
^^^^^
...
...
tests/httpwrappers/tests.py
Dosyayı görüntüle @
d0bd5330
...
...
@@ -504,6 +504,10 @@ class JsonResponseTests(SimpleTestCase):
response
=
JsonResponse
({},
encoder
=
CustomDjangoJSONEncoder
)
self
.
assertEqual
(
json
.
loads
(
response
.
content
.
decode
()),
{
'foo'
:
'bar'
})
def
test_json_response_passing_arguments_to_json_dumps
(
self
):
response
=
JsonResponse
({
'foo'
:
'bar'
},
json_dumps_params
=
{
'indent'
:
2
})
self
.
assertEqual
(
response
.
content
.
decode
(),
'{
\n
"foo": "bar"
\n
}'
)
class
StreamingHttpResponseTests
(
SimpleTestCase
):
def
test_streaming_response
(
self
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment