简体   繁体   中英

How can I get fixed output from my django database

Not so long time ago I started programming on Django Framework. I have a model Questions with 2 attributes - question_text and id . When my view works - in browser I see output smth like this:

[< Questions: First question >, < Questions: Second question >, < Questions:Third question >]

But I just want to see:

First question, Second question, Third question.

What should I do to fix it? Smth with __str__ function in model or what?

question = Questions.objects.all()

returns a queryset which you obviously print in your template. Try iterating over your queryset in your html template like:

{% for q in questions %}
   {{ q.question_text }}{% if not forloop.last %}, {% endif %}
{% endfor %}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM