In my django project in order to avoid another request I would like to return a JsonResponse with a HttpResponse in there like this:
JsonResponse({"known": True, "space": render(request, 'space.html')}, status=200)
Django process returns the normal internal error Object of type HttpResponse is not JSON serializable
I tried all the web solutions (serializer, etc.) and can not find a way to return a json format (necessary for my javascript) with dictionary entries with one of them being an entire html page that I can use with then with $("body").html(response["space"])
.
Am I missing something?
Thanks for your time.
The render
function returns a HttpResponse
, and indeed, the json
serializer does not know how to handle this.
You can instead use render_to_string
[Django-doc] to render it to a string instead:
from django.template.loader import
…
JsonResponse({
'known': True,
'space': 'space.html'
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.