1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #29082 -- Allowed the test client to encode JSON request data.

This commit is contained in:
Nick Sarbicki
2018-02-05 10:22:24 +00:00
committed by Tim Graham
parent d968788b57
commit 47268242b0
7 changed files with 86 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
import json
from urllib.parse import urlencode
from xml.dom.minidom import parseString
@@ -73,7 +74,20 @@ def post_view(request):
else:
t = Template('Viewing GET page.', name='Empty GET Template')
c = Context()
return HttpResponse(t.render(c))
def json_view(request):
"""
A view that expects a request with the header 'application/json' and JSON
data with a key named 'value'.
"""
if request.META.get('CONTENT_TYPE') != 'application/json':
return HttpResponse()
t = Template('Viewing {} page. With data {{ data }}.'.format(request.method))
data = json.loads(request.body.decode('utf-8'))
c = Context({'data': data['value']})
return HttpResponse(t.render(c))