I want to django query to javascript.so I changed queries to list. I sended list to javascript.But list were not displayed by console log.
class Comment_List_TV(ListView):
template_name = 'account/user_comment_list_tv.html'
def get_queryset(self):
Comment_list_query = list(Comment_tv.objects.none())
if self.request.user.is_authenticated:
Comment_list_query = list(Comment_tv.objects.filter(user=self.request.user))
print(Comment_list_query)
return Comment_list_query
Print display object list. But
const mydata = "{{Comment_list_query}}";
console.log(mydata);
Console log does not display django's object list. why? How do I send django object list to javascript?
CodePudding user response:
Console log does return nothing because Comment_list_query
in django template is called object_list
, so you should use...
const mydata = "{{ object_list }}";
console.log(mydata)
But it won't work, because you probably needs parsed values.
So, you should write view like this:
@csrf_exempt
def get_comments(request):
comment_list_query = list(Comment_tv.objects.all())
return JsonResponse({'result': comment_list_query})
And receive it from AJAX call (for example using AJAX method from jQuery package).