Creating a Javascript Object in Python/Django

Lets say you’ve got a view that handles your AJAX calls and you want to pass an javascript object back to your javascript callback. How to construct that object? Here it is:

from django.http import JsonResponse

def my_view(request):
    my_obj = {'item1': 'a', 'item2': 'b'}
    return JsonResponse(my_obj)

I think you can pass objects to template as part of context if you use json.dumps() to package the object.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s