I have 2 models:
class Category(models.Model): title = models.CharField(max_length=250) ### other fields class Album(models.Model): category = models.ForeignKey(Category) subject = models.CharField(max_length=200) ### other fields... .
I just wrote a view for filtering albums by specefic category, also I want them all in home.html template:
#views.py def commercial(request): commercial_subjects = Album.objects.filter(category__title__contains="commercial" ) return render(request, 'gallery/index.html', {'commercial_subjects': commercial_subjects}) And it works fine for just commercial category. It seems just like hardcoding if I want to write multiple views for each category like this one. What I need is a view or filtering process which shows all categories and their related album.subject automaticly. So the final result must be like this:
Personal
- ALBUM 1
- ALBUM 2
Commercial
- ALBUM 4
- ALBUM5
How can I do that?