[Answered ]-JSON data from Django to D3 graph

2👍

Getting data into the right format and ready to go is typically something you do in your model. Here is a trivial hypothetical example:

# In models.py:
class Address(models.Model):
    street = models.CharField(max_length=200)
    city = models.CharField(max_length=200)
    state = models.CharField(max_length=200)
    def get_full_address(self):
        return self.street + "\n" + self.city + ", " + self.state

Now in your view, pass the model instance into the template:

return render(request, 'address.html', {'address': address_object})

And in the template:

{{ address.get_full_address }}

… will return the textually formatted address.

So, if I wanted to supply this to a JavaScript jQuery function (again, for hypothetical demonstration) I could say:

<span id="address_label"></span>
<script>
    ...
    $("#address_label").html("{{ address.get_full_address }}");
    ...
</script>

Leave a comment