29👍
I think the canonical answer is: “Don’t”.
What you should probably do instead is unravel the thing in your view code, so it’s just a matter of iterating over (in|de)dents in the template. I think I’d do it by appending indents and dedents to a list while recursing through the tree and then sending that “travelogue” list to the template. (the template would then insert <li>
and </li>
from that list, creating the recursive structure with “understanding” it.)
I’m also pretty sure recursively including template files is really a wrong way to do it…
85👍
Using with
template tag, I could do tree/recursive list.
Sample code:
main template: assuming ‘all_root_elems’ is list of one or more root of tree
<ul>
{%for node in all_root_elems %}
{%include "tree_view_template.html" %}
{%endfor%}
</ul>
tree_view_template.html renders the nested ul
, li
and uses node
template variable as below:
<li> {{node.name}}
{%if node.has_childs %}
<ul>
{%for ch in node.all_childs %}
{%with node=ch template_name="tree_view_template.html" %}
{%include template_name%}
{%endwith%}
{%endfor%}
</ul>
{%endif%}
</li>
- [Django]-How to automatically login a user after registration in django
- [Django]-Retrieving parameters from a URL
- [Django]-How about having a SingletonModel in Django?
49👍
I’m too late.
All of you use so much unnecessary with tags, this is how I do recursive:
In the “main” template:
<!-- lets say that menu_list is already defined -->
<ul>
{% include "menu.html" %}
</ul>
Then in menu.html
:
{% for menu in menu_list %}
<li>
{{ menu.name }}
{% if menu.submenus|length %}
<ul>
{% include "menu.html" with menu_list=menu.submenus %}
</ul>
{% endif %}
</li>
{% endfor %}
- [Django]-Django – Overriding the Model.create() method?
- [Django]-What is related_name used for?
- [Django]-What's the difference between select_related and prefetch_related in Django ORM?
20👍
this might be way more than you need, but there is a django module called ‘mptt’ – this stores a hierarchical tree structure in an sql database, and includes templates for display in the view code. you might be able to find something useful there.
here’s the link : django-mptt
- [Django]-Django – what is the difference between render(), render_to_response() and direct_to_template()?
- [Django]-How to resize the new uploaded images using PIL before saving?
- [Django]-South migration: "database backend does not accept 0 as a value for AutoField" (mysql)
12👍
Yes, you can do it. It’s a little trick,
passing the filename to {% include %} as a variable:
{% with template_name="file/to_include.html" %}
{% include template_name %}
{% endwith %}
- [Django]-Django Queryset with year(date) = '2010'
- [Django]-How to filter empty or NULL names in a QuerySet?
- [Django]-Are Django SECRET_KEY's per instance or per app?
10👍
Django has a built in template helper for this exact scenario:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#unordered-list
- [Django]-Django: Get model from string?
- [Django]-Django FileField: How to return filename only (in template)
- [Django]-Django REST framework post array of objects
9👍
I had the same problem and I wrote a template tag. I know there are other tags like this out there but I needed to learn to make custom tags anyway 🙂 I think it turned out pretty well.
Read the docstring for usage instructions.
- [Django]-How to debug Django commands in PyCharm
- [Django]-How can I list urlpatterns (endpoints) on Django?
- [Django]-Django Many-to-Many (m2m) Relation to same model
1👍
Does no one like dicts ? I might be missing something here but it would seem the most natural way to setup menus. Using keys as entries and values as links pop it in a DIV/NAV and away you go !
From your base
# Base.html
<nav>
{% with dict=contents template="treedict.html" %}
{% include template %}
{% endwith %}
<nav>
call this
# TreeDict.html
<ul>
{% for key,val in dict.items %}
{% if val.items %}
<li>{{ key }}</li>
{%with dict=val template="treedict.html" %}
{%include template%}
{%endwith%}
{% else %}
<li><a href="{{ val }}">{{ key }}</a></li>
{% endif %}
{% endfor %}
</ul>
It haven’t tried the default or the ordered yet perhaps you have ?
- [Django]-Paginate relationship in Django REST Framework?
- [Django]-Bulk create model objects in django
- [Django]-How to set the timezone in Django
1👍
correct this:
root_comment.html
{% extends 'students/base.html' %}
{% load i18n %}
{% load static from staticfiles %}
{% block content %}
<ul>
{% for comment in comments %}
{% if not comment.parent %} ## add this ligic
{% include "comment/tree_comment.html" %}
{% endif %}
{% endfor %}
</ul>
{% endblock %}
tree_comment.html
<li>{{ comment.text }}
{%if comment.children %}
<ul>
{% for ch in comment.children.get_queryset %} # related_name in model
{% with comment=ch template_name="comment/tree_comment.html" %}
{% include template_name %}
{% endwith %}
{% endfor %}
</ul>
{% endif %}
</li>
for example – model:
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
# Create your models here.
class Comment(models.Model):
class Meta(object):
verbose_name = _('Comment')
verbose_name_plural = _('Comments')
parent = models.ForeignKey(
'self',
on_delete=models.CASCADE,
parent_link=True,
related_name='children',
null=True,
blank=True)
text = models.TextField(
max_length=2000,
help_text=_('Please, your Comment'),
verbose_name=_('Comment'),
blank=True)
public_date = models.DateTimeField(
auto_now_add=True)
correct_date = models.DateTimeField(
auto_now=True)
author = models.ForeignKey(User)
- [Django]-How do you catch this exception?
- [Django]-Django REST Framework: adding additional field to ModelSerializer
- [Django]-Cannot apply DjangoModelPermissions on a view that does not have `.queryset` property or overrides the `.get_queryset()` method
-2👍
I had a similar issue, however I had first implemented the solution using JavaScript, and just afterwards considered how I would have done the same thing in django templates.
I used the serializer utility to turn a list off models into json, and used the json data as a basis for my hierarchy.
- [Django]-Giving email account a name when sending emails with Django through Google Apps
- [Django]-How to run this code in django template
- [Django]-How do I use pagination with Django class based generic ListViews?