python - Model name is being displayed in Django template -
i'm new django i'm building few simple apps increase knowledge. trying display concatenated list, when display list shows model names so:
[<famousquote: yourself; else taken>][<infamousquote: . dunno. either way. >]
this views.py file:
def index(request): famous_quote = famousquote.objects.all().order_by('?')[:1] infamous_quote = infamousquote.objects.all().order_by('?')[:1] compiled = [famous_quote, infamous_quote] return render(request, 'funnyquotes/index.html', {'compiled': compiled})
and index.html file:
{% if compiled %} {{ compiled|join:"" }} {% else %} <p>no quotes you.</p> {% endif %}
is there i'm doing wrong, or better way this?
you have list of lists, unicode representation of list contains <objectname:string>
, if had list of model objects, you'd proper __unicode__
representation of objects.
ultimately, template automatically trying convert python objects string representations, in case of queryset
[<object: instance.__unicode__()>]
.
you have defined your desired string representation object instances - need make sure template engine receives instances - not other classes.
look @ difference in output in shell.
print(famousquote.objects.all().order_by('?')[:1]) # calls str(queryset) # vs print(famousquote.objects.order_by('?')[0]) # calls str(famousquote)
either update view
compiled = [famous_quote[0], infamous_quote[0]]
or template
{% quotes in compiled %}{{ quotes|join:"" }}{% endfor %}
tl;dr
you have lists of lists joining string representation of lists, not instances.
Comments
Post a Comment