Thank you very much guys! that issue has solved!(Here is my old post Django name 'admin' is not defined )
but it show nothing. I want to show the content in header.html & home.html
here is the code personal/views
from django.shortcuts import render
def index(request):
return render(request,'personal/templates/personal/home.html')
And here are the code of home and header
{% extends "personal/header.html" %}
{% block content %}
<p> Hey welcome to my very first project by Django :D </p>
{% include "personal/includes/"htmlsnippet.html %}
{% endblock %}
header:
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Harry-Phuc Coi</title>
<meta charset="utf-8">
</head>
<body class="body" style="background-color: #f6f6f6f6">
<div>
{% block content %}
{% endblock %}
</div>
</body>
</html>
And here is my path
my path
Firstly, note that you should specify the template relative to your templates directory, so you shouldn't need the personal/templates prefix.
return render(request,'personal/home.html')
Secondly you have " in the wrong place. It should be at the end of the template name.
{% include "personal/includes/htmlsnippet.html" %}
Related
I'm trying to make my first Django project. I'm making the first post and in the the tutorial, it goes like this in HTML:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1> This is our list of post</h1>
{% for post in post_objects &}
{{ post }}
{% endfor %}
</body>
</html>
Now that I'm trying to run the server and check the page so I can see the 'This is our list of post', there is an error.
Error during template rendering
In template C:\Users\Invoker\Dev\trydjango\src\posts\templates\posts\index.html, error at line 10
Invalid block tag on line 10: 'endfor'. Did you forget to register or load this tag?
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title></title>
5 </head>
6 <body>
7 <h1> This is our list of post</h1>
8 {% for post in post_objects &}
9 {{ post }}
10 {% endfor %}
11 </body>
12 </html>
What should I do?
Ciao Sadra!
I see, the error message is not helping at all. But I think it's just a typo!
{% for post in post_objects &}
At the end of the for block there's a & when there should be a %.
Template syntax: {% load %}.
You need to make the following changes.
{% for post in post_objects %}
I have some dynamic fields on the base.html like the footer text and the social accounts links, but I need to fetch them from the database and send them alongside with each response for a view that uses a template which extends the base.html template.
right now I am fetching the needed Items on every view and send them alongside with the view context, but I feel that this is repetitive, especially if I have more dynamic items, I tried also to save them to the request session but also it will require more code and edge cases.
what is the most efficient way to fetch these items once and be able to use them on all the views that extends the base.html template?
Alexei's answer is a good one, I just want to expand a little and say you can also use snippets for reusable chunks of HTML.
eg.
_snippet.html
<p>Some html code</p>
any_page_on_your_website.html
{% extends 'base.html %}
{% block head %}
<title>Page title</title>
{% include '_snippet.html' %}
{% endblock %}
{% block content %}
<p>page content</p>
{% endblock %}
I like to use this to seperate bits out by name, so I can easily find what I need to change. For example, you might have snippets called `_company_info.html', '_basic_footer_sitemap.html' etc. It would be easy enough to put this info into your header, footer, navbar etc. but I find having the named files makes for simpler maintenance.
UPD: if you prefer to use snippets in your case, please read the answer below from #urbanespaceman.
You can use template tags for your footer. The same as we use {% block head %} {% endblock head %} to insert to base.html unique meta title and description on each page.
Create a template tag in your footer and path any parameters in it from your views.
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
{% block head %}
<title></title>
<meta name="description" content="">
{% endblock head %}
</head>
<body>
{% block content %}
{% endblock %}
<footer>
{% block footer_content %}
{% endblock %}
</footer>
</body>
</html>
any_page_on_your_website.html
{% extends 'base.html' %}
{% block head %}
<title>Page title</title>
{% endblock %}
{% block content %}
<p>page content</p>
{% endblock %}
{% block footer_content %}
<p>My unique footer with dynamic variables from view: {{var1}}, {{var2}}</p>
{% endblock %}
views.py
def any_page_on_your_website(request):
var1 = SomeModel.objects.filter(foo=bar)
var2 = AnotherYourModel.objects.filter(foobar=barfoo)
thanks to #DanielRoseman he pointed me to the use of context_processor,
now I have created a context_processor that returns a dict which contains all the objects that I frequently ask for, so it is now accessible from anywhere on my templates.
I've created a base.html file where I want my bootstrap3 navbar and footer to live. These will be used on every page of my site.
However, the base.html & corresponding css file that goes with it seems to overwrite all of the index.html file and specific css for that view.
I've read the django documentation and closely-related questions like this one on overriding the base template. Other website have tutorials but still aren't making sense. I believe I am misunderstanding something fundamental.
Here is the code:
base.html:
<!DOCTYPE html> {% load staticfiles %}
<html>
<head>
<link rel="stylesheet" href="/static/css/main.css" />
<!-- jquery -->
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<!-- [/] jquery -->
</head>
<body>
{# Load the tag library #} {% load bootstrap3 %} {# Load CSS and JavaScript #} {% bootstrap_css %} {% bootstrap_javascript %} {# Display django.contrib.messages as Bootstrap alerts #} {% bootstrap_messages %} {# Navigation Menu #}
<header>
<nav class="navbar navbar-default">
----->Navbar code here<-----
</nav>
</header>
<footer>
<div class="container">
<p>Good stuff is here in the footer</p>
</div>
</footer>
</body>
</html>
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="description" content="Online community">
<meta name="author" content="My name">
<title>Planet</title>
<link href="/static/css/homepage.css" rel="stylesheet">
</head>
<body>
{% extends 'base.html' %}
{% block content %}
<p>WORDS WORDS WORDS WORDS</p>
<h1>HERE ARE SOME BIG WORDS ON THE MAIN PAGE</h1>
{% endblock content %}
</body>
</html>
I can include the css files for index.html & base.html if it helps but I believe the problem lies somewhere with my understanding of extending the base template and how to use {% block content %}. I can remove that block and it doesn't seem to matter either.
Thank you for any insights you can provide.
It looks like you're trying to use template extending
In simplicity, you should structure your files like so:
base.html
<head> </head>
<body>
{% block content %}
index.html will be loaded and everything within
the block named "content" will display here
{% endblock %}
</body>
<footer> </footer>
index.html
{% extends 'base.html' %}
{% block content %}
Everything within this block, named "content", will
be inserted into the "content" block of base.html
{% endblock %}
Your combined HTML would look like this once it passes through Django's templating system:
<head> </head>
<body>
Everything within this block, named "content", will
be inserted into the "content" block of base.html
</body>
<footer> </footer>
Your view will need to return the rendered index.html. This system is designed such that you can continue to use base.html with other templates to maintain a standard structure or page design, while modifying only the content on those pages with different versions of index.html.
I followed the Django "first app" tutorial, and now I am wondering how I could write templates that the whole site will use. I can write the templates for each view, but how do I write a template that will be on every page and then include the 'sub templates' in say a 'content block'?
See the Template Inheritance section of the Django template docs.
Basically, you create one template that is the "base":
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" href="style.css" />
<title>{% block title %}My amazing site{% endblock %}</title>
</head>
<body>
<div id="sidebar">
{% block sidebar %}
<ul>
<li>Home</li>
<li>Blog</li>
</ul>
{% endblock %}
</div>
<div id="content">
{% block content %}{% endblock %}
</div>
</body>
</html>
and save it as (for example) "base.html" in your template directory, then each specific page template looks like:
{% extends "base.html" %}
{% block title %}My amazing blog{% endblock %}
{% block content %}
{% for entry in blog_entries %}
<h2>{{ entry.title }}</h2>
<p>{{ entry.body }}</p>
{% endfor %}
{% endblock %}
Long story short, you should write a base template and then extend this template. This is called template inheritance and is well explained here
I do html/css by trade, and I have been working on and off django projects as a template designer. I'm currently working on a site that uses Jinja2, which I have been using for about 2 weeks. I just found out through reading the documentation that Jinja2 doesn't support multiple level template inheritance, as in you can't do more than one
{% extends "foo" %}
per rendering. Now I'm pretty sure you can do this in Django, which is powerful because you can specify a base template, specify 3 or 4 templates based on that, and then build the meat of your pages using those base templates. Isn't the point of inheritance so you have more power to abstract so your only really messing with unique code?
In any case I have no idea what to do here. I don't know if there is some way I can do it that will work as well as it could with the Django templates. I'm not exactly an expert at either Django or Jinja(2) but I can provide any information needed.
One of the best way to achieve multiple level of templating using jinja2 is to use 'include'
let say you have 'base_layout.html' as your base template
<!DOCTYPE html>
<title>Base Layout</title>
<div>
<h1>Base</h1>
.... // write your code here
{% block body %}{% endblock %}
</div>
and then you want to have 'child_layout.html' that extends 'base_layout.
{% include "base_layout.html" %}
<div>
... // write your code here
</div>
{% block body %}{% endblock %}
and now your page can just extends 'child_layout.html' and it will have both base_layout.html and child_layout.html
{% extends "child_layout.html" %}
{% block body %}
...// write your code here
{% endblock %}
The way the documentation worded it, it seemed like it didn't support inheritance (n) levels deep.
Unlike Python Jinja does not support
multiple inheritance. So you can only
have one extends tag called per
rendering.
I didn't know it was just a rule saying 1 extends per template.... I now know, with some help from the jinja irc channel.
Try this, this work for me thanks to #Ixm answer.
base.html
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
{% block content %}{% endblock %}
</body>
</html>
content.html
{% extends "base.html" %}
{% block content %}
<table>
<tr>
{% include "footer.html" %}
</tr>
</table>
{% endblock %}
footer.html
{% block footer %} <td> test</td>{% endblock %}
and call with
env = Environment(loader=FileSystemLoader(os.path.join(path, "Layouts")))
template = env.get_template('content.html')
html = template.render()
print html
After struggling for a long time, I found {{super}} for multiple levels of inheritance in jinja2 templates.
The following is inspired from https://stackoverflow.com/a/31093830/1300775.
base.html
<html>
<body>
{% block title %}
Brand
{% endblock %}
</body>
layer-1.html
{% extends "base.html" %}
{% block title %}
{{ super() }} - Section
{% endblock %}
layer-2.html
{% extends "layer-1.html" %}
{% block title %}
{{ super() }} - Article
{% endblock %}
Rendering template layer-2.html will output Brand - Section - Article in block title.
I recently faced the same issue. I wanted to inherit several child templates and it worked. To illustrate it I would like to show you a solution that worked for me:
I had a base.html file that has block content and extended by manage.html. and that manage.html has a block sub_manage which is extended by internet_market.html, so visually it looks like:
|- base.html (block content)
|--manage.html (extends base.html)
|---sub_manage.html (extends manage.html)
when I rendered it, everythink worked fine, which means that you can have several {% extends %} in one render. the only thing is that if you are using relative links to your css or js files then it might not work, rather it will render, but it won't find your css/js files.
like:
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="../static/css/bootstrap.min.css">
<script type="text/javascript" src="../static/js/bootstrap.min.js"></script>
<style type="text/css">
</head>
In that case you have to use dynamic links by using url_for. like:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="{{url_for("static", filename = "css/bootstrap.min.css")}}">
<script type="text/javascript" src="{{url_for("static", filename = "js/bootstrap.min.js")}}"></script>
<style type="text/css">
See the documentation extending, including, and importing.
This provides the means of getting functionality from multiple files for different purposes and is different from the depth of the nesting.
You can perfectly have a template that extends a template that extends a template...
Multiple inheritance and multiple-level inheritance are not the same. I understand the question is related to the latter.
Let me show my workaround for the problem:
parent-template.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Your Title</title>
<link rel='stylesheet' href="{{ url_for('static', filename='css/main.css') }}">
{% block head %}{% endblock %}
</head>
<body>
{% block nav %}{% endblock %}
{% block body %}{% endblock %}
</body>
</html>
child-template.html
{% extends 'parent-template.html' %}
{% block nav %}
<header>
<div>
<nav>
...
[navbar html code]
...
</nav>
</div>
</header>
{% endblock %}
login.html (where I don't need navbar)
{% extends 'parent-template.html' %}
{% block body %}
<header>
...
[header html code]
...
</header>
<main>
...
[main html code]
...
</main>
{% endblock %}
home.html (where I need navbar)
{% extends 'child-template.html' %}
{% block body %}
<main>
...
[main html code]
...
</main>
{% endblock %}
Both login.html and home.html uses all the data from parent-template, but only home.html uses data from child-template (the navbar).
You could use the following way to combine different contents into a single layout.html for various layout designs:
{% if instance == 'type1' %}
{% elif instance == 'type2' %}
{% else %}
{% endif %}
...and call:
render_template('layout', instance='%s' % instance)
in python code.