[Answered ]-Get objects assigned to ForeignKey

1๐Ÿ‘

I strongly advise you to back to the document to read about ForeignKey: https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_one/

anyway, you can follow that:

#views.py

def Category(request, slug):
    category_user = Category.objects.get(slug=slug)
    category_video = Video.objects.filter(category=category_user)
    categories = Category.objects.all()
    dict = {'category_user' : category_user, 'category_video' : category_video, 'categories' : categories}
    return render(request, 'categories.html', dict)

I just modified the capital of the "c" character in the filter of video model

#html

{% for video in category_video %} 
    {% for cats in video.category_set.all %}
        {{ cats.<fieldname> }}
    {% endfor %}
{% endfor %}

Leave a comment