In View:
context['categories'] = = models.Category.objects.all().get_cached_trees()
In template:
{% load mptt_tags %}
<ul>
{% recursetree categories %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
As a result, it renders only first level of queryset. If remove get_cached_trees it renders all tree. How to render all tree with get_cached_trees?
You don't actually need to call get_cached_trees() in this situation because recursetree already does the caching for you.
From the documentation:
Iterates over the nodes in the tree, and renders the contained block for each node.
This tag will recursively render children into the template variable {{ children }}.
Only one database query is required (children are cached for the whole tree)
Related
In my Django project I use django-mptt application to create hierarchical tree. Right now next code works well but I want to show only first 4 level of the tree. How to make it correctly? I am confused.
views.py:
context['caregories'] = Category.objects.get(id=5).get_descendants()
html:
{% load mptt_tags %}
<ul>
{% recursetree caregories %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
You can filter the descendants by their level
obj = Category.objects.get(id=5)
context['caregories'] = obj.get_descendants().filter(level__lte=obj.level + max_depth)
where max_depth is the depth you require
What is the correct syntax for the href on an html page with Jinja2 code, that allows for navigation between two pages? The first html template has a list of names, while the other has person details.
Here is the code I have for the listOfNames.html page that displays the list of names.
<ul>
{% for rownum, row in listNames.iterrows() %}
<li>{{ row.firstName }} {{ row.lastName }}</li>
{% endfor %}
</ul>
Here is the server.py code that gets/puts (correct use of term?) the names on the listOfNames.html.
#app.route('/listNames/<bo>/')
def listNames(bo):
listNames = getListNames(bo)
return render_template('listOfNames.html', listNames=listNames)
This is code for the personInformation.html.
<main role="main" class="col-sm-9 ml-sm-auto col-md-10 pt-3">
<h1>{{ person.firstName }}
{{ person.lastName }}
</h1>
<h2>Office:
{{ person.bo }}
</h2>
<h2>Courses Completed
</h2>
<ul>
{% for rownum, row in personCompleted.iterrows() %}
<li><a href="/courses/{{row.courseId}}">
{{row.courseTitle}}
</a></li>
{% endfor %}
</ul>
</main>
And here is the server.py code.
#app.route('/people/<person_id>')
def person(person_id):
person = getPersonOrganization(person_id)
personCompleted = getPersonCompleted(person_id)
return render_template('personInformation.html', person=person, personCompleted=personCompleted)
The best way to do this is by using the url_for() function from Flask. Here is your new listOfPeople.html template with the link:
<ul>
{% for rownum, row in listNames.iterrows() %}
<li>{{ row.firstName }} {{ row.lastName }}</li>
{% endfor %}
</ul>
It's best to not hardcode your URLs in the templates, because if you ever need to reorganize them, then you would need to update URLs all over the place. With url_for() Flask takes care of generating the URLs for you using the information you provided in the app.route decorators.
I'm trying to have two separate menus in my django-cms app. One for the header and another with a different set of links for the footer:
[ Logo ] Link_A Link_B Link_C Link_D
... content ...
Link_E Link_F Link_G Link_H
Using baked in {% show_menu %}, will show all of the pages registered, links A - H, and doesn't allow me to separate the two menus.
How can I create two separate menus?
Depends what you want to do really, but I've got a base template which has a navigation menu at the top and a sitemap submenu at the bottom.
So starting with the navigation;
{% show_menu 1 100 100 100 "partials/navigation.html" %}
Which uses the template;
{% load cms_tags menu_tags cache cms_page %}
{% for child in children %}
<li>
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">
{{ child.get_menu_title }}
</a>
{% if child.children and child.level <= 4 %}
<ul>
{% show_menu from_level to_level extra_inactive extra_active template '' '' child %}
</ul>
{% endif %}
</li>
{% endfor %}
Then the sitemap;
{% show_sub_menu 2 1 1 "partials/sitemap.html" %}
And sitemap.html
{% load cms_tags cms_page cache %}
{% if children %}
{% for child in children %}
<ul class="site-footer__column">
<li>
<h4>
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">
{{ child.get_menu_title }}
</a>
</h4>
</li>
{% if child.children %}
{% for baby in child.children %}
<li class="footer_sub">
<a href="{{ baby.attr.redirect_url|default:baby.get_absolute_url }}">
{{ baby.get_menu_title }}
</a>
</li>
{% endfor %}
{% endif %}
</ul>
{% endfor %}
{% endif %}
Understanding the options (numbers) you can provide for a menu can enable you to display different parts of your site, but if the built in menu tags don't suit your needs, you could write a custom menu tag.
The standard menu docs are here; http://docs.django-cms.org/en/3.2.2/reference/navigation.html
And here are the docs for customising your menus; http://docs.django-cms.org/en/3.2.2/how_to/menus.html
I am wondering how to create a jinja2 template that would allow my to put my header into a variable where I can extend the base file and then call upon the header in my child file.
Currently my code for the parent is:
{% block head %}
<div class="wrapper col2">
<div id="topbar">
<div id="topnav">
<ul>
<ul>
<li>Home</li>
<li>Sign in/up</li>
<li>Admin console
<ul>
<li>Console</li>
<li>Staff Management</li>
<li>ALERTS</li>
<li>Sign up Shooters</li>
</ul>
</li>
<li >Contact Us</li>
<li class="last">logout</li>
</ul>
</div>
<br class="clear" />
</div>
<hr>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }} </li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</div>
{% endblock %}
My child template is:
{% extends "Header.html" %}
{% block head %}
{% endblock %}
<h1>CREATE SHOOT</h1>
<form action="" method="post" name="form">
{{ form.hidden_tag() }}
<p>
<h2>Name of shoot:</h2>
{{ form.nameofshoot(size=40) }}<br>
<h2>Number of days in shoot:</h2>
{{ form.day}}
<p><input type="submit" value="Create Shoot"></p>
</form>
Am I doing something wrong or is there another approach I can take?
What you need to do is remove from your child template the lines {% block head %}{% endblock %}
Because in your child template you call {% extends "Header.html" %}, all of the content declared in Header.html (no matter what blocks it is in) will automatically be included. That's the job of extends.
By adding the {% block head %}{% endblock %} in your child template, what you have effectively said is "take everything from Header.html, but override everything in the 'head' block with what I specify in my child template". Then, because there is nothing in the block defined in your child template, you are replacing the content defined in your Header.html file with nothing.
You want to use the {% block blockname %} notation to specify what you want to change in the child template, not what you want to keep the same.
This page on inheritence is the relevant section of the Jinja documentation, and provides a nice example.
Hay, I'm using MPTT to create some tree-like data from a model which contains conversations, and i want them to be ordered by a 'votes' field.
The model looks like this at the moment, very basic.
class Thread(MPTTModel):
message = models.CharField(max_length=100)
parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
votes = models.IntegerField()
class MPTTMeta:
order_insertion_by=['votes']
As you can see, we have a message field, and parent FK which is linked to the Thread model, and a votes.
Within my views i have this
threads = Thread.tree.all()
data = {
'threads':threads
}
return render_to_response("show.html",data )
then within my template
{% load mptt_tags %}
<ul class="root">
{% recursetree d %}
<li>
{{ node.title }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
However, the outputted list is a a list of all the threads. None of them are linked together.
Any ideas?
{% load mptt_tags %}
<ul class="root">
{% recursetree nodes %}
<li>
{{ node.message }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
In views I have:
threads = Thread.tree.all()
data = {
'nodes':threads
}
return render_to_response("show.html",data )
In the html page I see a tree of nodes order alphabetically and with indentation.