简体   繁体   中英

Django Rest Framework: How to show serialized value in django template?

I am trying to show the serialized data in the Django template. I am using Django Rest framework. Using Ajax I have just tried to show the serialized data in a textarea in the html template view.

In view.py, at first, I tried to make a dictionary from a list. And then send it to the serializer.py to make it JSON data. After doing this I just send that serialized data. And from the template, I am using ajax to get this data and set this value in a textarea which is get by ID identifier.

But it does not show the value in the textarea. In the textarea, it just prints "[object Object]". So, how do I show the serialized data in the textarea?

Here is my view.py:

@csrf_exempt
def result(request):
    text = request.POST['test']
    triples = getTriples(text)

    keys = ['sub','predi','obj']
    demo_data = dict(zip(keys,triples[0]))
    serializer = SentenceListSerializer(data=demo_data)

    if serializer.is_valid():
         serializer.save()
return JsonResponse(serializer.data, safe=False)

Ajax code:

$.ajax({
      method: 'POST',
      dataType: 'json',
      async: true,
      data: {'test': info},
      success: function(response_data){
        $('#textareashow').val(response_data);

      }
    });

You can use JSON.stringify(response_data)

Inside your ajax success function place this code $('#textareashow').val(JSON.stringify(response_data));

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM