It's a common practice to generate content different from standart HTML
output by HttpResponce. Lets consider Example on how to generate JSON
response by Django.
First of all! Django has built in "other response generators". Most common task is to generate JSON. In general it's extremely easy. Operation called "seriliazation". Official Django docs say:
" Django’s serialization framework provides a mechanism for “translating” Django objects into other formats. Usually these other formats will be text-based and used for sending Django objects over a wire, but it’s possible for a serializer to handle any format (text-based or not). "
Anyway an example, helped me a lot.
First of all! Django has built in "other response generators". Most common task is to generate JSON. In general it's extremely easy. Operation called "seriliazation". Official Django docs say:
" Django’s serialization framework provides a mechanism for “translating” Django objects into other formats. Usually these other formats will be text-based and used for sending Django objects over a wire, but it’s possible for a serializer to handle any format (text-based or not). "
Anyway an example, helped me a lot.
from django.utils import simplejson
def some_view(request):
result = []
result.append({"user":request.user})
result.append({"key":request.session.key})
return HttpResponse(simplejson.dumps(result), mimetype='application/json')
No comments:
Post a Comment