Jinja not rendering in HTML file - html

This is probably some sort of silly misunderstanding, but I'm trying to include some Jinja code in my HTML file, as in this file which I found on github:
{% extends "bootstrap/base.html" %}
{% block title %}
Demo App
{% endblock %}
{% block styles %}
{{super()}}
<link rel="stylesheet" href="{{url_for('.static', filename='starter-
template.css')}}">
{% endblock %}
{% block content %}
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Login</li>
<li>Sign Up</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<div class="starter-template">
<h1>Bootstrap starter template</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</div>
</div><!-- /.container -->
{% endblock %}
You'll see that the Jinja code is enclosed in the "{% ... %}." However, when I run this file in my web browser, I get the following:
As you can see at the top of the screenshot, instead of executing the Jinja code, the Jinja is being read as HTML text. How can this be fixed?
Thank you!

the jinja templates are not supposed to be viewed in browsers. flask will generate and serve proper html, generated from your templates, when requested.

Related

How to add css files to your Flask Application?

I have a flask application that runs fine but I want to add my own css files to make it more custom.
Here is my project structure
my_app
-app.py
static/
-style.css
templates/
-index.html
Here is my index.html:
{% extends "bootstrap/base.html" %}
{% block html_attribs %} lang="en"{% endblock %}
{% block title %} My_APP {% endblock %}
{% block styles %}
{{super()}}
<link rel="stylesheet" type="text/css" href="{{url_for('static', filename='style.css')}}">
{% endblock %}
{% block navbar %}
<div class="navbar navbar-expand-lg navbar-dark bg-dark" style="background-color:DodgerBlue" role="navigation" >
<div class="containter">
<div class="navbar-header">
<button type="button" class="navbar-toggle"
data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar">Toggle Navigation</span>
<span class="icon-bar">Toggle Navigation</span>
<span class="icon-bar">Toggle Navigation</span>
</button>
<a class="navbar-brand" href="{{ url_for('home_page') }}">ISE App</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li> Home </li>
<li> Add </li>
<li> Bulk Load </li>
<li> Delete </li>
<li> Bulk Remove </li>
</ul>
</div>
</div>
</div>
{% endblock %}
{% block content %}
<div class="containter">
<h3 id="testing">Hi how are you!!</h3>
{% block page_content %}<h3 id="testing1">Hi how are you!!</h3>{% endblock %}
</div>
{% endblock %}
Here is my css file named style.css
#testing {color:blue;}
#testing1 {color:red;}
div.containter{background-color:red}
Here is my page output:
As you can see, the two h3 tags are not blue or red and the containter background color is not red.
Any idea as to why my index.html is not references my style sheet? I am new to flask so any ideas or suggestion would be highly appreciated.
Try:
{% block styles %}
{{super()}}
<link rel="stylesheet" type="text/css" href="{{url_for('.static', filename='style.css')}}">
{% endblock %}

CSS and HTML Not Working In Flask Jinja2 Template

I have a fairly simple HTML template that I cannot get to work. It's baffling me.
I'm using a Jinja2 Template in a flask app to display a page using Twitter Bootstrap:
This is the code that I have:
{% extends "layout.html" %}
{% block description %}{{ description }}{% endblock %}
{% block title %}{{ title }}{% endblock %}
{% block content %}
<div class="container">
<h2 class="text-center" style="margin-top: 2rem;">Webinars on the Cutting Edge of Data Science</h2>
<p class="text-center">Two hour webinars on the latest topics in data science. We break down things like the newest in data science research, deep learning, and the newest techniques within the field.</p>
<hr>
<div class="row">
<div class="col-md-8">
<h3>Next Episode: {{ current_webinar['title'] }}</h3>
<hr>
<p><strong>Date:</strong> {{ current_webinar['date'] }}<br> <strong>Time: </strong> 6:30-8:30 EST</p>
<p><strong>Description: </strong>{{ current_webinar['description'] }}</p>
Learn More
</div>
<div class="col-md-4">
{% set current_webinar_img = current_webinar['path'][7:] | replace('\\', '/') %}
<img src="{{ url_for('static', filename=current_webinar_img) }}" height="350" width="100%">
</div>
</div>
<div class="row">
<h3 class="text-center">My Heading Make It Longer To See What Happens</h3>
<p>This is my paragraph</p>
Learn More
</div>
</div>
{% endblock %}
The issue I have is that all of the material in the second row class does not show up correctly.
I would expect that block to have the title followed by a paragraph, but instead everything appears on one line:
I find this bizarre and can't detect what's causing the changes. It is a simple template and as far as I can tell there is no error in my html/css, and I'm not aware of any problems in my jinja template that would cause things to render incorrectly.
FWIW here is the HTML of the entire page once it's rendered:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="Two hour webinars held every two weeks on data science">
<meta name="author" content="Jonathan Bechtel">
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/css/styles.css">
<link rel="stylesheet" href="/static/css/jumbotron.css">
<title>Live data science webinars</title>
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="/">Home<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Teaching</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
<a class="dropdown-item" href="/data-science-part-time-class">Part Time Class</a>
<a class="dropdown-item" href="/webinars">Webinars</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="/contact">Contact</a>
</li>
</ul>
</div>
</nav>
<main role="main">
<div class="container">
<h2 class="text-center" style="margin-top: 2rem;">Webinars on the Cutting Edge of Data Science</h2>
<p class="text-center">Two hour webinars on the latest topics in data science. We break down things like the newest in data science research, deep learning, and the newest techniques within the field.</p>
<hr>
<div class="row">
<div class="col-md-8">
<h3>Next Episode: Tensorflow Deep Dive, Vol. 2</h3>
<hr>
<p><strong>Date:</strong> 2020-07-08<br> <strong>Time: </strong> 6:30-8:30 EST</p>
<p><strong>Description: </strong>In this workshop, we'll learn the fundamentals and basics of Tensorflow.</p>
Learn More
</div>
<div class="col-md-4">
<img src="/static/img/life_sciences.png" height="350" width="100%">
</div>
</div>
<div class="row">
<h3 class="text-center">My Heading Make It Longer To See What Happens</h3>
<p>This is my paragraph</p>
</div>
</div>
</main>
<footer class="container">
<p>© Company 2017-2020</p>
</footer>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js#1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="/static/js/bootstrap.min.js"></script>
</body>
</html>
IMO the code is correct. Make sure the /static/css/styles.css doesn't contain any bad CSS definitions, then try to clear the browser cache.

CSS: navbar items on top of each other?

in my page the navbar menu items show on top of each other.
Why?
Codepen:
https://codepen.io/ogonzales/pen/mdeNNLB
Code:
<nav class="navbar navbar-expand-md navbar-light fixed-top" style="height: 70px;" id="top-navbar">
<img src="" alt="Ministerios Elim" width="2%" height="50%"/>
<a class="navbar-brand" href="" style="margin-right: 5%;"><span style="margin-left: 3%;">Ministerios Elim</span></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse"
aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul>
<li>
Home
</li>
{% for item in navigation.menu_items.all %}
<li>
<a href="{{ item.link }}" class="nav-link" {% if item.open_in_new_tab %} target="_blank" {% endif %}>{{ item.title }}</a>
</li>
{% endfor %}
</ul>
<form class="form-inline ml-auto">
Ingresar
Registro
</form>
</div>
</nav>
This is the default behaviour of items inside an unordered list (ul), which are block elements. You need to make them inline-block if you want them to appear on one line.
For example, try adding this code to your CSS:
#navbarCollapse ul li {
display: inline-block;
}
You will see that the items are no longer all underneath each other.
If the options still don't all appear on one line, you are probably having the problem (at least it looks to me like you would, from your codepen you have provided) that the width of your container is not large enough to fit all the elements in it.
Consider making this wider, or displaying less navigation options if design constraints and screen sizes don't allow any more.
You can add "display: inline-block" to your <li>

Bootstrap 3 Navbar not working on mobile

My Bootstrap Navbar isn't working on mobile phone view (when it can't display the links). I can't click the button. After many research on Internet and StackOverflow, I tried out the solution and no one works. These are the solutions that I tried;
Verify the data-target and id. As here Check
Add the <meta name="viewport" content="width=device-width, initial-scale=1.0"> Here Check
Add CSS .dropdown-backdrop { position: static; } On Stack, somewhere
Here is my navbar code;
<nav id="layout-nav" class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navigationbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">7 Frères</a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse" id="navigationbar">
<ul class="nav navbar-nav">
{% for item in staticMenu.menuItems %}
<li class="{% if item.isActive %}active{% endif %}">{{ item.title }}</li>
{% endfor %}
</li>
</ul>
</div>
</div>
</nav>
Thanks!
The issue is that you are not including the Bootstrap Javascript(necessary for using the built-in dropdown menu) on your page. Once you have included that, it should work:
<script tpye="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

Bootstrap navbar hamburger is opening but not closing

On a phone sized screen, when I click the hamburger once, the navbar opens fine. When I try to close it, nothing happens - it's stuck. Where have I gone wrong? Here's my html:
<header class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#teams-navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://{{base_url}}">My Teams</a>
</div>
<nav class="collapse navbar-collapse" id="teams-navbar-collapse">
<ul class="nav navbar-nav">
{% if user.is_authenticated %}
<li>
<a href="{% url 'profile_page' user.pk %}">
My Profile
</a>
</li>
<li>
Logout
</li>
</ul>
{% else %}
{% block login_button %}{% endblock %}
{% endif %}
</nav>
</div>
</header>
I would say this is actually an issue with jQuery Toggle. Please ensure jquery is set to toggle "collapsed". One of the ways bootstrap works is by adding/removing classes to buttons/divs/html elements to display them based on css. For example. Pretty Select dropdowns work by adding "open" to a specific div before or after the select button. Hense you can use jQuery to toggle that open when it's clicked. I hope this helps.
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#teams-navbar-collapse">
<script type="text/javascript">
$( document ).ready(function() {
$("button.navbar-toggle").click(function(){
$("button.navbar-toggle").toggleClass("collapse");
});
});
</script>