Passing JSON data to the front end using Django

后端 未结 5 2053
旧巷少年郎
旧巷少年郎 2021-01-01 02:42

Is there a way to pass JSON objects to the front end of a web template if using the Django framework or Python in general?

For example, if I want to send an object t

5条回答
  •  被撕碎了的回忆
    2021-01-01 03:13

    Complementing zeekay answer, if you want to send only an object you could do a json dump, for example:

    import json
    
    def my_ajax_view(request):
        if not request.is_ajax():
            raise Http404
    
        data_dict = getmydata() #lets supose is a dict
        return HttpResponse(json.dumps(data_dict))
    

    That way you will receive that data via your ajax success and do whatever you want with it.

    You can send over lists too, when you receive your response sometimes you will need to do a JSON.parse on data (sometimes cause when you send a dictionary I think is not necessary)

提交回复
热议问题