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
Related
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" %}
...
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.
I'm writing an application in which user can choose one of several tools for data analysis and open it in a panel on main page. Is it possible to use django "extends" and have each tool defined in different file?
The minimal example of what im strugling with would be like this:
base.html
<div>
{% block left_panel %}
left block
{% endblock content%}
</div>
<div>
{% block right_panel %}
right block
{% endblock %}
</div>
and sample left_panel and right_panel tools:
left1.html
{% extends "base.html" %}
{% block left_panel %}
<p>TEST left 1</p>
{% endblock %}
right1.html
{% extends "base.html" %}
{% block right_panel %}
<p>TEST right 1</p>
{% endblock %}
Is there a way to render the base.html with both blocks overwriten?
I believe that the best way to implement your requirement is to create a new template that extends base.html and includes left1.html and right1.html. Something like this:
{% extends "base.html" %}
{% block left_panel %}
{% include "left1.html" %}
{% endblock content%}
{% block right_panel %}
{% include "right1.html" %}
{% endblock %}
Update based on OP's comment: Actually you just need one configurable template, not 100. Let's say that based on the tools the user selects, your view passes the left_tool and right_tool context variables to your template. Now, you can easily do something like this:
{% block left_panel %}
{% if left_tool == "tool1" %}
{% include "left1.html" %}
{% elif left_tool == "tool2" %}}
{% include "left2.html" %}
etc ...
{% else %}
{% include "left10.html" %}
{% endif %}
{% endblock content%}
You'll do the same with the right panel. Of course the above is a little naive and definitely not DRY -- instead you could for instance generate the name of the template to be included in the view and pass it directly to the template, or use a custom node etc.
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.
So what I want to do is add the django admin header within my own base template for my project. I copied over the base.html from admin templates to my project. Can I somehow put {% block header %} tags within base.html and then call it within my own base template for my project?
{% block header %}
<!-- Header -->
<div id="header">
<div id="branding">
{% block branding %}{% endblock %}
</div>
{% if user.is_active and user.is_staff %}
<div id="user-tools">
{% trans 'Hi,' %}
<strong>{% filter force_escape %}{% firstof user.first_name user.username %}{% endfilter %}</strong>.
{% block userlinks %}
{% url 'django-admindocs-docroot' as docsroot %}
{% if docsroot %}
{% trans 'Documentation' %} /
{% endif %}
{% trans 'Change password' %} /
{% trans 'Log out' %}
{% endblock %}
</div>
{% endif %}
{% block nav-global %}{% endblock %}
</div>
<!-- END Header -->
{% endblock %}
All templates that extend from 'base.html' would contain the content inside {% block header %} and {% endblock %}, as long as they do not override the block or its ancestors(by removing the {% block header %}...{% endblock %} part).
If there are templates that do not extend from base.html, you could put the code into their common base, or use something like inline tag or inclusion tags
Also, for the code to work properly for authenticated users, you need to ensure there is user variable in context: normally it's already there, or you need to re-enable "django.contrib.auth.context_processors.auth" if you've removed it before, check the doc
I ended up extending my main template from the admin 'base.html' template and going from there. A little messy but it works