Flask is rendering the head of my html document twice - html

I'm having trouble trying to get Flask (along with Flask-Bootstrap) to only produce one head element to my HTML document. The problem I'm having right now is that the head element is correct, but Flask is dropping it into the beginning of the body as well.
/personal_website/app/templates/index.html:
{% extends "bootstrap/base.html" %}
#! I have not changed bootstrap/base.html
{% block head %}
{{super()}}
{% block title %}My_Name | Home{% endblock title %}
{% block styles %}
{{super()}}
<link href="{{url_for('static',filename='stylesheets/style.css')}}"
rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto"
rel="stylesheet">
{% endblock styles %}
{% endblock head %}
Console output:
<head>
<title>My_Name | Home</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/stylesheets/style.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>
<body>
My_Name | Home
<!-- Bootstrap -->
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/stylesheets/style.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">

I actually was able to correct this by taking the contents of what was in the header (ex: Title and Styles) out of the block head.
/personal_website/app/templates/index.html:
{% extends "bootstrap/base.html" %}
{% block title %}My_Name | Home{% endblock %}
{% block styles %}
{{super()}}
<link href="{{url_for('static',filename='stylesheets/style.css')}}"
rel="stylesheet">
{% endblock styles %}
{% block head %}
{{ super() }}
{% endblock %}
This still alowed me to inherit the contents of the head from bootstrap/base.html

I think you just need to remove this line (probably both times):
{{super()}}

Related

Include multiple templates with their CSS and JavaScript codes

How can I include the following template (there are many of such template) in the main template so that each block gets appended to the corresponding block in the main template? Actually I want to avoid adding CSS and JavaScript separately each time I include a template, so asking this question.
{% block head %}
<link rel="stylesheet" href="/static/navbar.css">
{% endblock %}
{% block body %}
<div id="navbar"></div>
{% endblock %}
{% block script %}
<script src="/static/navbar.js"></script>
{% endblock %}
Main template:
<!DOCTYPE html>
<html>
<head>
{% block head %}
<link rel="stylesheet" href="/static/main.css">
{% endblock %}
</head>
<body>
<div id="header"></div>
{% block body %} {% endblock %}
<div id="footer"></div>
{% block script %}
<script src="/static/main.js"></script>
{% endblock %}
</body>
</html>

How to display separate titles on each page in Django

I have a Django website where I have separated the html files into a base.html file as so:
{% include 'head.html' %}
<body>
{% include 'nav.html' %}
{% block content %}
{% endblock content %}
{% include 'footer.html' %}
{% include 'scripts.html' %}
</body>
</html>
Due to including head.html, the title on each page is the same, since head.html has only 1 title. Here is the head.html file:
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'css/materialize.css' %}">
<link rel="stylesheet" href="{% static 'css/materialize.min.css' %}">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<link rel="stylesheet" href="{% static 'css/custom.css' %}">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<title>mytitle</title>
</head>
But i want to display different titles for different pages and I dont know how to. Anyone have any idea?
base.html
{% include 'head.html' with title=title %}
<body>
{% include 'nav.html' %}
{% block content %}
{% endblock content %}
{% include 'footer.html' %}
{% include 'scripts.html' %}
</body>
</html>
views.py
def home(request):
context = {
"title":"Home"
}
return render(request,"template",context)
head.html
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'css/materialize.css' %}">
<link rel="stylesheet" href="{% static 'css/materialize.min.css' %}">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<link rel="stylesheet" href="{% static 'css/custom.css' %}">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<title>{{title}}</title>
</head>
Use blocks which are overridable:
head.html
...
<title>{% block page_title %}{% endblock %}</title>
my_concrete_page.html
{% extends base.html %}
{% block page_title %}my concrete title{% endblock %}
I am giving this answer from my knowledge:
Make one file for this :
head.html
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'css/materialize.css' %}">
<link rel="stylesheet" href="{% static 'css/materialize.min.css' %}">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<link rel="stylesheet" href="{% static 'css/custom.css' %}">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
make different file for your different title :
title1.html
<title>mytitle</title>
title2.html
<title>mytitle</title>
now add in your main file like this :
<head>
{% include 'head.html' %}
{% include 'title1.html' %}
</head>
<body>
{% include 'nav.html' %}
{% block content %}
{% endblock content %}
{% include 'footer.html' %}
{% include 'scripts.html' %}
</body>
</html>
I hope this works for you.
use include instead of extend for base.html and pass dynamic title to base.html
django link : include
{% include "base.html" with objects=website.title %}
I had to combine the ideas of #Ivan and #Soham. I removed the title tag from my head.html and added that to my base.html. Along with that I used overrideable block tag inside the title tag. So now my base.html looks like this:
<!DOCTYPE html>
<html lang="en">
{% include 'head.html' %}
<title>{% block title %}{% endblock title %}</title>
<body>
{% include 'nav.html' %}
{% block content %}
{% endblock content %}
{% include 'footer.html' %}
{% include 'scripts.html' %}
</body>
</html>
And all I have to do now is use the tags accordingly in other pages:
{% extends 'base.html' %}
{% block title %}whatever i want the title to be{% endblock title %}
Option 1. Base.html file
<link rel="stylesheet" href="{% static 'css/style.css' %}"
.
.
<title>{% block title %} Home-title {% endblock %}</title>
Then add like this code your file
{% block title %} Search Results {% endblock %}
Option 2. To query a model in the view:
{% extends "blog/base.html" %}
{% block title %} {{blog_obj.title}} {% endblock %}
{% block content %}

Django Diplay Data leafleft

I am learning djnago , I would like to display an html page (in template forlder) into another html file by keeping {% extends 'base.html' %} which my template HTML that has my nav bar, CSS , javascritp..
the structure:
App1=>templates=>App1=>map.html (is a map app html file generated with folium https://python-visualization.github.io/folium/)
App1=>templates=>App1=>home.html
Src=>templates=>base.html
in home.html I would like to diplay map.html and all base.html elements (nav bar, CSS, javasript)
Here is my base .html code :
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- semantic UI -->
<!--Chart js-->
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<title>skill-site | {% block title %} {% endblock title %}</title>
</head>
<body>
{% include 'navbar.html' %}
{% block scripts %}
{% endblock scripts %}
<div class="container ui">
{% block content %}
{% endblock content %}
</div>
</body>
</html>
here is the code for home.html but it is not working:
{% extends 'base.html' %}
{% block title %}my map{% endblock title %}
{% block content %}
{% include './map.html' %}
{% endblock content %}
Thanks for your help

yii2 twig child extends, parent layout output twice

I'm Twig beginner. I currently try to setup Yii2 layout with Twig.
I having 3 Twig file.
base.twig
{{ this.beginPage() }}
<!DOCTYOE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="{{ app.language }}">
<head>
<meta charset="{{ app.charset }}" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ html.encode(this.title) }}</title>
{{ this.head() }}
{{ html.csrfMetaTags() | raw }}
</head>
<body>
{{ this.beginBody() }}
{% block body %}{% endblock %}
{{ this.endBody() }}
</body>
</html>
{{ this.endPage() }}
cp.twig
{% extends '_layouts/base.twig' %}
{% block body %}
<div class="cp">
{% block header %}{% endblock %}
<hr />
{% block content %}
{{ content }}
{% endblock %}
</div>
{% endblock %}
index.twig
{% extends '_layouts/cp.twig' %}
{% block header %}this is header{% endblock %}
After I render from my Yii2 controller return $this->render('index'); then it come out very weird output:
<!DOCTYOE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<meta name="csrf-param" content="_csrf">
<meta name="csrf-token" content="z_y1CPsfUBwHE6uC3dHg225wVQ8i2-eGhZa9f55Qrm39nYI6gk0TdVJ6nruf5q6WJAcCaUGolPWo9fxM_TrNHA==">
</head>
<body>
<div class="cp">
<hr />
<!DOCTYOE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link href="/mlaxology/yii2/workspace/projects/pawscms/web/assets/903cba57/css/style.css" rel="stylesheet">
<meta name="csrf-param" content="_csrf">
<meta name="csrf-token" content="z_y1CPsfUBwHE6uC3dHg225wVQ8i2-eGhZa9f55Qrm39nYI6gk0TdVJ6nruf5q6WJAcCaUGolPWo9fxM_TrNHA==">
</head>
<body>
<div class="cp">
this is header <hr />
</div>
</body>
</html>
</div>
</body>
</html>
It output unexpected duplicated parent layout.
My objective is to create a dynamic layout header which can be override by child layout with Twig {% block header %}{% endblock %} command. I know there is other way to solve this issue, like using Twig variable to store the dynamic header. But I like they way what {% block %} does.
I'm wondering why base.twig {% block body %}{% endblock %} working properly but index.twig having repeat content issue ....
I already spend 2 days on this, try googling but no similar issue can be found ... finally I think I really need stackoverflow help.
Sorry for my English and thanks for reading this :)
My ugly way to solve this issue :
base.twig:
{% if content is defined %}{{ content | raw }}{% else %}
{{ void(this.beginPage()) }}
<!DOCTYOE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="{{ app.language }}">
<head>
<meta charset="{{ app.charset }}" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ html.encode(this.title) }}</title>
{{ this.head() }}
{{ html.csrfMetaTags() | raw }}
</head>
<body>
{{ this.beginBody() }}
{%- block body %}{% endblock %}
{{ this.endBody() }}
</body>
</html>
{{ void(this.endPage()) }}
{% endif %}
After several debug ... this happen because reprint content, so I wrap with {% if content is defined %}{{ content | raw }}{% else %} for temp fix.
Any better way to solve this issue?

linking css file to base html docuemnt - django

I have a django project and i have been trying to link a css file to the base.html document to begin implimenting some css to my file. I have bootstrap linked already but i want to add more customization... Here is what i have so far..
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<link href="{% static 'css/blog.css' %}" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
</head>
<body>
<div class="container-fluid blue5">
{% block content %}
{% endblock %}
</div>
</body>
</html>
here is the css file
.blue5 {
background-color: lightblue,
}
You can link a CSS and HTML file like this:
In your HTML code:
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<link href="{% static 'css/blog.css' %}" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<link href="pathToYourCSSfile" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
</head>
<body>
<div class="container-fluid blue5">
{% block content %}
{% endblock %}
</div>
</body>
</html>
Hope this helps.
if your css file only contains these two lines it will be better to include them in the head of the document
<head>
...
<script ...>
<style>
.blue5 {
background-color: lightblue,
}
</style>
</head>
otherwise just like the others css, add a new link tag:
<link href="{% static 'path to your file.css' %}" rel="stylesheet">