I am learning about template inheritence in django
base.html:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>leshav</title>
</head>
<body>
<h1>My helpful timestamp site</h1>
{ % block content % }{ % endblock % }
<footer>
<hr>
<p>thanks</p>
</footer>
</body>
</html>
dateTime.html:-
{% extends "base.html" %}
{% block content %}
<p>today is {{ d|date:'d-m-y' }}.</p>
{% endblock %}
view for the function related to this page's url:-
def current_datetime_block(request):
now = datetime.datetime.now()
return render(request, "dateTime.html",{'d':now})
the problem is that it is not reading the content in the dateTime.html and is returning base.html file as it is
output:-
My helpful timestamp site
{ % block content % }{ % endblock % }
thanks
This is the output when I run the server
where the problem is coming? please help me
if anything else about the program is needed, you can ask me about it
Related
I was following one chatbot tutorial using Django, I came across this error:
frontpage.html
{% extends 'core/base.html' %}
{% block title %}Welcome | {% endblock %}
{% block content %}
<div class="p-10 lg:p-20 text-center">
<h1 class="text-3xl lg:text-6xl text-white">Djangochat</h1>
</div>
{% endblock %}
base.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> {% block title %}{% endblock %}Django </title>
<script src="https://cdn.tailwindcss.com" ></script>
<body class="bg-teal-600">
<nav class="flex items-center justify-between px-4 py-6 bg-teal-800">
<div>
Djangochat
</div>
<div class="flex items-center space-x-4">
Log in
Sign up
</div>
</nav>
{% block content %}
{% endblock %}
</head>
</body>
</html>
Output
enter image description here
So, {% block title %} {% end block %} is not working, kindly help.
Change the title to this on base.html
<title>{% block title %}my base page{% endblock %}</title>
Then go to frontpage.html or any page you want to
<title>{% block title %}my front page{% endblock %}</title>
As mentioned in the official documentation The Django template language
EDIT
You're mistaken about the form of HTML the close tag of the head in the body
Try this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> {% block title %}{% endblock %}Django </title>
<script src="https://cdn.tailwindcss.com" ></script>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
I need to change the title of base.html based on rendered page in the
{% block content %}
You have to add first the "Block Title" on base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My app - {% block title %}{% block title %}</title>
</head>
<body>
{% block content %}{% block title %}
</body>
</html>
and then you can change it when you call it
{% extends 'base.html' %}
{% load static %}
{% block title %}My new title{% endblock title %}
{% block content %}
<p>My new content</p>
{% endblock content %}
My webpage just prints out "{% extends 'base.html'% }". I'm wanting it to just print out the header "Template".
Both html files are in templates and the py file is one directory outside of that.
app.py:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
if __name__ == "__main__":
app.run(debug=True)
index.html:
{% extends 'base.html' %}
{% block head %}
{% endblock %}
{% block body %}
<h1>Template</h1>
{% endblock %}
base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
{% block head %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
Django version 2.1.3
Python version 3.7
Writing out sample code just to get an understanding of Django. Right now I'm on Templates and I'm having 0 luck when it come to rendering variables.
In views.py folder I've created a little dictionary and passed it through under the variable content
from django.shortcuts import render
# Create your views here.
posts = [
{
'Title': 'Challanger 1',
'Name': 'Sammy',
'Age': '33',
'Food': 'Seafood'
},
{
'Title': 'Challanger 2',
'Name': 'Sammy',
'Age': '33',
'Food': 'Seafood'
}
]
def home(request):
content = {
'posts': posts
}
return render(request, 'blog/home.html', content)
In my home.htmlfile, I've added a few 123 next to my {{variable}}to make sure the .html file is connecting to the view.py. When I py manage.py runserver, only the 123 is displayed but none of my {{variables}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{% for post in posts %}
<p>123 {{ post.name }}</p>
<h1>123 {{ post.title }}</h1>
<h1>123 {{ post.age }}</h1>
{% endfor %}
</body>
</html>
localhost:8000 produces:
123
123
123
123
123
123
When I open view-source from the browser:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>123 </p>
<h1>123 </h1>
<h1>123 </h1>
<p>123 </p>
<h1>123 </h1>
<h1>123 </h1>
</body>
</html>
Side note: When I'm in the home.html file I've noticed that only {{post.title}} and {{post.name}} auto-fill and {{post.age}} and {{post.food}} never auto-fill.
There are also times when I've deleted everything in the home.html file and none of the {{variables}} auto-fill when I rewrite out the code. Either way the ending result is still the same, variables wont load.
You're using post.name (all lowercase) in the template, but you defined the dictionaries in the python code to have Name (uppercase N) as the key.
Try using {{ post.Name }}.
Here is the problem, Dictionary Keys Are Case-Sensitive.
try below code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{% for post in posts %}
<p>123 {{ post.Name }}</p>
<h1>123 {{ post.Title }}</h1>
<h1>123 {{ post.Age }}</h1>
{% endfor %}
</body>
</html>
I have a base.html file from where I extend other pages. I have this code in my base.html:
base.html
<html lang="es-es">
<head>
<title>
Home - {% block titulo %}{% endblock %}
</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/style/main.css" rel="stylesheet" />
</head>
All the templates extending from base.html have some spanish words and symbols and I have to put this meta tag again (so it doesn't throw me a unicode error), even when is on the base.html file:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
Aren't the templates supposed to inherit all the tags from the parent template? What is it that I'm doing wrong?
--EDIT 16 Mar 2016--
This is one example of a child. For the view I'm using: ListView.as_view in the url file, but not all the files extending from the base are using the same method although I do have to insert the meta tags in all the files for accents and other symbols to work.
{% extends "base.html" %}
{% load static %}
<!--The next 2 lines after this comment
are in every html files that extends from the base file
-->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% block titulo %}
Lideres
{% endblock%}
{% block content %}
<h2>Listado de Teléfonos</h2>
{% endblock %}