Once you have created categories in your app, you need to display them hierarchically in a template. The easiest way to do this is to use the {% recursetree %}
template tag from the django-mptt
app. I will show you how to do that in this recipe.
Make sure that you have the Category
model created and some categories entered in the database.
Pass QuerySet
of your hierarchical categories to the template and then use the {% recursetree %}
template tag as follows:
Create a view that loads all the categories and passes them to a template:
# movies/views.py # -*- coding: UTF-8 -*- from __future__ import unicode_literals from django.shortcuts import render from .models import Category def movie_category_list(request): context = { "categories": Category.objects.all(), } return render( request, "movies/movie_category_list.html", context )
Create a template with the following content:
{# templates...