Custom Django Wagtailmenus Flatmenu Template - django

I am using https://github.com/rkhleics/wagtailmenus for my Django Wagtail menus, but can't seem to figure out how to use a custom template for my flat_menu. I followed the guides but I think I may be doing something wrong.
My flat_menu template is in a directory menus/top_sub_menu.html, where top_sub_menu is the handle of the menu I created.
top_sub_menu.html
{% load menu_tags %}
{% if menu_items %}
<ul class="c-links c-theme-ul">
{% for item in menu_items %}
<li>
{{ item.text }} {% if item.has_children_in_menu %}{% sub_menu item %}{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
header.html
{% load menu_tags %}
...
{% flat_menu 'top_sub_menu' %}
...
I have a custom main_menu.html and a sub_menu.html in the same directory and they work, so I know my menu directory is in correct location. Thank you.

The template location behaviour described in the README for the {% flat_menu %} tag (https://github.com/rkhleics/wagtailmenus#4-using-the--flat_menu--tag) was only introduced in version 2.2.0. You should be able to use your custom template by utilising the template argument, though. For example:
In header.html
{% load menu_tags %}
...
{% flat_menu 'top_sub_menu' template="menus/top_sub_menu.html" %}
...

Related

Failed template inheritance in django templating

I am in a middle of a project. The project uses basic html at the frontend. I having trouble in template inheritance.
This is the basic code : -
{% extends 'main.html' %}
{% block content %}
<h2>Home</h4>
<hr>
{% if request.user.is_authenticated %}
{% block home %}{% endblock home %}
{% else %}
{% for doc in doctor %}
<div>
<small>Doctors around</small>
<br>
<li>{{doc.user.name}}</li>
<br>
</div>
{% endfor %}
{% endif %}
{% endblock content %}
Also the code is extended to another template.
The child page is :-
{% extends 'rec/home.html' %}
{% block home %}
<div>
{% if request.user.usertype == 'p' %}
<h1>Hi {{request.user.name}} </h1>
{% else %}
<h1>Hi {{request.user.name}} </h1>
{% endif %}
</div>
{% endblock home %}
Both the files are in the same directory. But i have defined the templates dir in settings file in a different directory.
When i do tree /a at the templates directory this is what i get :-
Folder PATH listing
Volume serial number is 6A82-72DF
E:.
\---rec
It's just {% endblock %} you don't have to specify what block you're closing / I'm not sure you even can Wrong
That's the only issue I see with what's provided
Instead of defining the template to extend from in each template, maybe try doing something like this:
Template.html
{% extends parent_template %}
Views.py
template = loader.get_template('app/page.html')
context = {}
context["parent_template"] = "app/parentPage.html"
return HttpResponse(template.render(context, request))
This way you can assign the template from the Django side and it should be easier to troubleshoot

Is there any way to show a field on a listing page in Wagtail admin?

I know that I can register my model as a Django model and add an image there but I need to save a pages' tree and identify my pages by an image on a listing page. To make a long story short, I need to show images on the listing page in admin.
Create a template in templates/wagtailadmin/pages/listing/_page_title_explore.html
{% load i18n wagtailadmin_tags %}
{# The title field for a page in the page listing, when in 'explore' mode #}
<div class="title-wrapper">
{% if page.sites_rooted_here.exists %}
{% if perms.wagtailcore.add_site or perms.wagtailcore.change_site or perms.wagtailcore.delete_site %}
{% endif %}
{% endif %}
{% if page_perms.can_edit %}
{{ page.get_admin_display_title }}
{% else %}
{{ page.get_admin_display_title }}
{% endif %}
{% block pages_listing_title_extra %}
{% if page.youtube_video_id %}
<img src='https://i.ytimg.com/vi/{{page.youtube_video_id}}/mqdefault.jpg' width='150'/>
{% endif %}
{% endblock pages_listing_title_extra %}
{% include "wagtailadmin/pages/listing/_privacy_indicator.html" with page=page %}
{% include "wagtailadmin/pages/listing/_locked_indicator.html" with page=page %}
</div>
<ul class="actions">
{% page_listing_buttons page page_perms %}
</ul>
Pay attention on {% block pages_listing_title_extra %}, for example, you can add your variable from a model to this block.
Keep in mind that the admin template has been overwritten, and after upgrading the Wagtail core you may not see some new features.
If you just want to add image, you don't need to override entire page title explore template. You can use just pages_listing_title_extra block.
Create file templates/wagtailadmin/pages/listing/_page_title_explore.html with content:
{% extends "wagtailadmin/pages/listing/_page_title_explore.html" %}
{% load wagtailimages_tags %}
{% block pages_listing_title_extra %}
{% image page.image fill-100x200 %}
{% endblock pages_listing_title_extra %}
This should not break with new version of Wagtail (only if this template block would be removed/renamed).

Django template within a for loop

I have a for loop that looks like this:
<ul>
{% for post in latest_post_list %}
<li>{{ post }}</li>
</ul>
{% empty %}
<p>No posts are available.</p>
{% endfor %}
But I want to wrap each post object in a template, so for example:
{% for post in latest_post_list %}
{% include 'jobposttemplate.html' %}
{% empty %}
{% include 'noposts.html' %}
{% endfor %}
jobposttemplate.html will then include all the various information held within the JobPost model.
I tried:
{% for jobposttemplate.html in latest_post_list %}
{{ jobposttemplate.html }}
{% empty %}
{% include 'noposts.html' %}
{% endfor %}
But, predictably, it didn't work. Basically I'm just trying to avoid having to having to write the html out each time, e.g.:
{% for post in latest_post_list %}
<div style="blah">{{ post.deadline }}
</div>
<div style="schmah">{{ post.created_at }}
</div>
{% empty %}
<p>No posts are available.</p>
{% endfor %}
If you are using app specific subfolders inside your templates/ directory, you need to add them to the file name here too.
{% include 'myapp/jobposttemplate.html' %}
And if you want the template to have access to variables, you can add them to the template's scope using with. The sub-template also has access to all scope variables too.
{% include 'myapp/jobposttemplate.html' with object=post %}

Djangocms template not showing up

I have a basic DjangoCMS up and running.
base.html contains:
{% block content %}{% endblock content %}
I also have feature.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block title %}{% page_attribute "page_title" %}{% endblock title %}
{% block content %}
<div>
{% placeholder "feature2" %}
</div>
<div class="jumbotron"">
{% placeholder "feature" %}
</div>
<div>
{% placeholder "content" %}
</div>
{% endblock content %}
I added the "feature2" placeholder in the above, and it correctly displays for editing on the site.
I then added a new line to base.html:
{% block base_logo %}{% endblock base_logo %}
and created a new file, base_logo.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block base_logo %}
<div>
{% placeholder logo %}
</div>
{% endblock base_logo %}
I expected this to also appear on the site for editing, but it doesnt. I have added the base_logo.html to the CMS_TEMPLATES in settings.py and TEMPLATE_DIR is also pointing correctly.
What else do I need to do for Djangocms to pick up my new template?
Take a look at template inheritance.
You're trying to use two {% extends %} tags, which won't work. You should use the {% include %} tag for base_logo, because it seems you'd want to include this in many templates. This question provides more info.

extend other app to my template working

I want to extend an other template to my blog.html, no matter have i try to extend this, it doesn`t work .
blog/index.html
{% block nav %}
<ul id="nav">
<li>{% block nav-blog %}Blog{% endblock %}</li>
<li>{% block nav-photo %}Photo{% endblock %}</li>
</ul>
{% endblock %}
<div class="news">
{% block polls %}
{% extends 'polls/index.html' %}
{% endblock %}
<div>
polls/index.html
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
Usually extends has to be stuck at the top of the template and not somewhere in the template.
In the template you use the tags that you have set up in the base templates.
You need to make sure you're doing two things here:
Declare a "nav" block in your index.html. This will let django know the part of your index html you wish to have overwritten when a template extends it. Anything you put inside the "nav" block in index html will be considered default content.
You need to use the "extends" template tag at the beginning of your blog.html so django knows the template you'd like to extend.
Like this:
index.html
{% block nav %}
<div>
Some default html here..
</div>
{% endblock %}
And then blog.html
{% extends index.html %}
{% block nav %}
<div>
This is what will render
</div>
{% endblock %}
Also, you're going to want to test index.html before trying to extend it to verify that django is finding that template. If it isn't then you need to add the directory that it is found in to the TEMPLATE_DIRS array in your settings.py file.