I have created a Django application with a model with a TextField
. When I use the admin interface, I can populate the TextField
as such below:
However, when I render it in JSON using a template I get the following on my browser. I.e. it cannot handle the line breaks correctly as such:
I am not sure how to handle this correctly, so that the text from my text field can be typed as required within the admin interface, and then rendered correctly as JSON.
Here is a snippet from my model.py:
@python_2_unicode_compatible
class Venue(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=50, blank=False, null=False)
description = models.TextField(blank=False, null=False)
def __str__(self):
return self.name
Here is the function in the views.py
def venues(request):
venues_list = Venue.objects.order_by('-name')
context = {'venues_list':venues_list}
return render(request, 'myapp/venues.json', context, content_type='application/json')
Here is my venue.json template:
[
{% for venue in venues_list %}
{
"venue_id":"{{venue.id}}",
"name":"{{venue.name}}",
"description":"{{venue.description}}"
}
{% if forloop.last %}{% else %}, {% endif %}
{% endfor %}
]
Any help appreciated?
P.S. Not sure if a template is a good approach. But I want to control what fields get displayed in the JSON data, not just JSON dump the entire model.