how to pass python json object to django template
Question: I want to pass some data(dict format) from views to template. And then in the
template I want to transform it to json format, so that it can be used in
javascript. But I tried to use json filter, it turns out it is not a valid
filter in django. I also tried to use pass a json data in the context
dictionary like this, but I get an error in the template, it says the data is
not JSON serializable. i know there is a way to make a another ajax call just
to get json data. But I want to get the data in the initial call.

    
    
        items = models.Model1.objects.all()
        itmes = json.dumps(items)
        return render(request, "index.html", {"items":items})
    

The template I try to use is

    
    
    <script type="text/javascript">
        var data = {{ items}};
        $('#table').DataTable( {
            data: data,});
    </script>
    
    # model 
    class Model1(models.Model):
       token = models.CharField(max_length=100)
       flag = models.BooleanField(default = False)
    


Answer: You don't have to pass it to template and then transform it to json. You can
directly return a string response.

    
    
    import json
    return HttpResponse(json.dumps({"item":items}))
    

