Здравствуйте, dev, в этой категории Я научу вас, как делать категории, подразделения и субпо категории Django в простых шагах.
Post Link: – https://webdevcodes.com/python/how-to-make-a-sub-and-sub-sub-categories-in-django/
LinkedIn: – https://www.linkedin.com/in/shivamrohillaa/
Github Repo:- https://github.com/ShivamRohilllaa/django- categories-tree
Категории Джанго
Django Sub категории
Django sub sub
Сделайте модель для категорий Django.
class Category(models.Model): parent = models.ForeignKey('self', related_name='children', on_delete=models.CASCADE, blank = True, null=True) title = models.CharField(max_length=100) slug = AutoSlugField(populate_from='title', unique=True, null=False, editable=False) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return self.title class Meta: #enforcing that there can not be two categories under a parent with same slug # __str__ method elaborated later in post. use __unicode__ in place of unique_together = ('slug', 'parent',) verbose_name_plural = "categories" def __str__(self): full_path = [self.title] k = self.parent while k is not None: full_path.append(k.title) k = k.parent return ' -> '.join(full_path[::-1])
Написать код в просмотр .py
def home(request): catg = Category.objects.filter(parent=None) context = {'catg':catg} return render(request, 'index.html', context)
Теперь сделайте все в своих шаблонах
Сделайте файл index.html и начните получать детали в HTML-файле.
Спасибо
Шивам Роилла |. Разработчик Python
Оригинал: “https://dev.to/shivamrohilla/how-to-make-a-sub-and-sub-sub-categories-in-django-most-important-216p”