The problem: I have a file base.html, where I have a {% block content %}, for the page's content and a {% inlcude %}, for the navigation bar (bootstrap) of the base template. But the include does not find the Template (TemplateDoesNotExist) and I am sure I didn't spell it wrong. Do I have to provide anything for the template to be found or what?
base.html important content:
<body>
{% include "navbar.html" %}
<div>
{% block content %}
{% endblock %}
</div>
</body>
Some important information:
I downloaded bootstrap in the base.html file; base.html and navbar.html are in the same folder.
Since in your directory structure, you have home directory inside templates. Please change code as per below :
<body>
{% include "home/navbar.html" %}
<div>
{% block content %}
{% endblock %}
</div>
</body>
Related
Folder Structure:
mysite
-theme
--templates
---main_base.html
---theme_footer.html
---theme_menu.html
-home
--templates
---home
----main.html
main.html:
{% extends "main_base.html" %}
{% block content %}
blah blah
{% end content %}
main_base.html:
{% load static tailwind_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
{% tailwind_css %}
</head>
<body class="bg-blue-100">
<nav>
{% block navbarn %}
{% endblock %}
</nav>
{% block content %}
{% endblock %}
<footer>
{% block footer %}
{% endblock %}
</footer>
</body>
</html>
theme_menu.html:
{% extends "main_base.html" %}
{% block navbarn %}
home
{% endblock %}
theme_footer.html
{% extends "main_base.html" %}
{% block footer %}
<h1>this is a footer</h1>
{% endblock %}
So I was able to setup Django with Tailwind following the instructions on the plugin page. But I can't get the base theme to show multiple blocks. It doesn't show the menu nor the footer, just the base html template with content from main.html. Can't get it to work!
If anyone else is running into this issue, you can't use multiple extends. You instead, include it in your base.
For me, I removed the {% extends %} tags from the ancillary pages, and then included them in my theme_base.html like:{% include 'theme_footer.html' %}
after i'm using include tag in django template, i'm getting error.
error:
RecursionError at /home/
maximum recursion depth exceeded
Error during template rendering
my base.html file .
<html>
{% load static %}
<some static css here>
{% include "inc/header.html" %}
{% block header %} HEADER {% endblock header %}
<some static js here>
</html>
my header file is
{% extends 'base.html' %}
<p> test header</p>
You are right now doing two things at the same time, you are extending 'base.html' file, while in the same time you are including 'header.html'.
This is why you get recursion problems.
For things to work properly you need to remove {% include %} block of code. So your header file looks like
{% extends 'base.html' %}
{% block header %}
<p> Test Header </p>
{% endblock %}
While your base.html doesnt need 'HEADER' beetwen your {% block header %} tags, so it should look like:
<html>
{% load static %}
<some static css here>
{% block header %}{% endblock %}
<some static js here>
</html>
Sometimes I get an empty string returned for my include tags which is impossible, because I have some static elements in it.
This is happening sometimes in my productive environment. How can I debug such an issue?
For example my footer or header disappears in some cases (Which I include within my "base.html") which I can't reproduce.
Thx
Example:
base.html
<html>
...
{% include "subdir/_header.html" %}
...
{% block content %}
<h1>Default Content</h1>
{% endblock %}
...
{% include "subdir/_footer.html" %}
</html>
home.html
{% extends "base.html" %}
{% block content %}
<h1>Home related Content</h1>
{% endblock %}
So if I load home.html my footer sometimes disappears. No idea why, no errors.
FOUND THE PROBLEM
A missing static file caused that kind of problem. Unfortunately I did not get an error.
I don't know the exact reason of this issue, but can you try with {% extends 'base.html' %}.
The difference between include and extends right here:
{% include %} vs {% extends %} in django templates
Your base html should be Extended and not Included... include is for small blocks of codes, like components... blog post, news, contact and that stuffs
base html
{% load static %}
<!DOCTYPE html>
<html lang="en-US">
{% include 'path/head.html' %}
{% block content %}{% endblock %}
{% include 'path/footer.html' %}
</html>
other pages
{% extends 'base.html' %}
{% load static %}
{% block content %}
<!-- Content from each page -->
{% endblock %}
Obs.: Include inside of includes dont work propely, you can get the data from first page in includes inside includes... for that i usualy use templatetags or context_processors, depending of your need.
Use django-debug-toolbar to debug your entire application in dev environment, you can see the entire request and the includes that you want
https://django-debug-toolbar.readthedocs.io/en/stable/
I am using Django 1.9 and demo template structure to display home.html.
I have included the following in the base.html file:
{% block bootstrap3_content %}
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
...
</div>
</nav>
<div class="container-fluid">
{% autoescape off %}{% bootstrap_messages %}{% endautoescape %}
{% block content %}(no content){% endblock %}
</div>
{% endblock %}
Navbar shows successfully but I do not get the Bootstrap message in the container below?
I am a new user to Django and this App as well. Please help!
Edit1:
bootstrap.html extends bootstrap3.html; base.html extends bootstrap.html; home.html extends base.html.
home.html
{% block title %}My_title{% endblock %}
{% block content %} This is <em>bootstrap3</em> for <strong>Django</strong>.
{% endblock %}
I get the title in the navbar but no message inside content block.
Make sure that you have
{% load bootstrap3 %}
at the top of your page since you are using this module. Make sure that you have
'bootstrap3',
in your installed apps.
From your code it looks like you are extending a template. If you are, be sure to have a
{% extends FooTemplate.html %}
in case you are loading boostrap3 in the parent template.
#base.html
<html>
<head><title>Hello world</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
#child.html
{% extends base.html %}
{% block content}
This is the content that
comes here
{% endblock %}
but the html output of base.html not displaying content.? Why this template language not working ?
Template inheritance includes the parent template in the child, not the other way around.
Render child.html and you'll see your content surrounded by the base.html (parent) markup.
Also, you need to quote the parent template name:
{% extends "base.html" %}
{% block content %}
Content!
{% endblock %}