'Why do I lose bootstrap styling when passing variables through URL in Flask?
When I pass a variable in Flask my Bootstrap styling doesn't work. If I use a normal app route without the variable the styling works.
The relevant code is as follows:
app.py:
@app.route('/edit_customer/<c_id>')
def edit_customer(c_id):
#customer = queries.get_customer(c_id)
return render_template('edit_customer.html')
My base template:
<!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, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<link href="static/css/styles.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js" crossorigin="anonymous"></script>
{% block head %}
{% endblock %}
</head>
<body>
<nav class="sb-topnav navbar navbar-expand navbar-dark bg-dark">
<!-- Navbar Brand-->
<a class="navbar-brand ps-3" href="index.html">Company Admin Portal</a>
<!-- Sidebar Toggle-->
<button class="btn btn-link btn-sm order-1 order-lg-0 me-4 me-lg-0" id="sidebarToggle" href="#!"><i class="fas fa-bars"></i></button>
<!-- Navbar Search-->
<form class="d-none d-md-inline-block form-inline ms-auto me-0 me-md-3 my-2 my-md-0">
<div class="input-group">
<input class="form-control" type="text" placeholder="Search for..." aria-label="Search for..." aria-describedby="btnNavbarSearch" />
<button class="btn btn-primary" id="btnNavbarSearch" type="button"><i class="fas fa-search"></i></button>
</div>
</form>
<!-- Navbar-->
<ul class="navbar-nav ms-auto ms-md-0 me-3 me-lg-4">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdown" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i class="fas fa-user fa-fw"></i></a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#!">Settings</a></li>
<li><a class="dropdown-item" href="#!">Activity Log</a></li>
<li><hr class="dropdown-divider" /></li>
<li><a class="dropdown-item" href="#!">Logout</a></li>
</ul>
</li>
</ul>
</nav>
<div id="layoutSidenav">
<div id="layoutSidenav_nav">
<nav class="sb-sidenav accordion sb-sidenav-dark" id="sidenavAccordion">
<div class="sb-sidenav-menu">
<div class="nav">
<div class="sb-sidenav-menu-heading">Core</div>
<a class="nav-link" href="index.html">
<div class="sb-nav-link-icon"><i class="fas fa-tachometer-alt"></i></div>
Dashboard
</a>
<div class="sb-sidenav-menu-heading">INTERFACE</div>
<a class="nav-link" href="/customers">
<div class="sb-nav-link-icon"><i class="fas fa-table"></i></div>
Customers
</a>
<a class="nav-link" href="/bids">
<div class="sb-nav-link-icon"><i class="fas fa-table"></i></div>
Bids
</a>
<a class="nav-link" href="/workorders">
<div class="sb-nav-link-icon"><i class="fas fa-table"></i></div>
Work Orders
</a>
<a class="nav-link" href="invoices">
<div class="sb-nav-link-icon"><i class="fas fa-table"></i></div>
Invoices
</a>
{%if session['email'] == '[email protected]'%}
<a class="nav-link" href="/admin">
<div class="sb-nav-link-icon"><i class="fas fa-table"></i></div>
Admin
</a>
{%endif%}
</div>
</div>
<div class="sb-sidenav-footer">
<div class="small">Logged in as:</div>
Start Bootstrap
</div>
</nav>
</div>
<div id="layoutSidenav_content">
<main>
<div class="container-fluid px-4">
{% block content %}
{% endblock %}
</div>
</main>
<footer class="py-4 bg-light mt-auto">
<div class="container-fluid px-4">
<div class="d-flex align-items-center justify-content-between small">
<div class="text-muted">Copyright © Company 2021</div>
<div>
<a href="#">Privacy Policy</a>
·
<a href="#">Terms & Conditions</a>
</div>
</div>
</div>
</footer>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="js/scripts.js"></script>
</body>
</html>
The edit_customer page that loses the styling:
<!DOCTYPE html>
<html lang="en">
{% extends "base.html" %}
{% block head %}
<title>Test | Test</title>
{{ super() }}
<style type="text/css">
.important { color: #336699; }
</style>
{% endblock %}
{% block content %}
<h1>Edit Customers</h1>
{% endblock %}
Solution 1:[1]
Try including your styles.css with a dynamic route like this
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
Solution 2:[2]
- How are you making the call to the backend (your server)? You should post the code
- You don't actually need the section below in your edit_customer page because it already exists in base.html which you are extending
<!DOCTYPE html>
<html lang="en">
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | NoCommandLine |