[Answer]-Model name is being displayed in Django template

1👍

You have a list of lists, so the unicode representation of the list contains the <ObjectName:string>, where if you had a list of model objects, you’d get a proper __unicode__ representation of your objects.

Ultimately, the template is automatically trying to convert python objects into its string representations, which in the case of the QuerySet is [<object: instance.__unicode__()>].

You have clearly already defined your desired string representation for object instances – you just need to make sure the template engine receives those instances – not other classes.

Look at the difference in output in a shell.

print(FamousQuote.objects.all().order_by('?')[:1])   # calls str(QuerySet)
# vs
print(FamousQuote.objects.order_by('?')[0]) # calls str(FamousQuote)

Either update your view

compiled = [famous_quote[0], infamous_quote[0]]

Or your template

{% for quotes in compiled %}{{ quotes|join:"" }}{% endfor %}

TL;DR

You have lists of lists so you are joining the string representation of the lists, not the instances.

Leave a comment