[Fixed]-How to display the name field of Category model instead of [<Category: hockey>, <Category: run>]

1đź‘Ť

âś…

You should use a for in loop in your template, to display the name for each category, try this :

{% for cat in category.category_set.all %}<a href="{{ cat.get_absolute_url }}">{{ cat.name }}</a> {% endfor %}

But I assumed that category.category_set.all was the way you get the set of categories to traverse. I’m not sure that this is the correct way to do this (if may be a way do to this I don’t know though).

Why don’t you get all categories from your view and passing the set directly as something like “categories” to your render() call?

With something like this :

def myview(request):
    cats = Category.objects.all()
    render(request, "mytemplate.html", {"categories" : cats})
👤vmonteco

Leave a comment