How to link a css file in python - python

I'm sending a message with HTML via Python. Now, I'd like to style it, I've tried to write the style code into the html but there are problems because curling braces {}. Can I link the css file in Python?
<!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">
<link href="https://fonts.googleapis.com/css?family=Lato:400,900i&display=swap" rel="stylesheet">
<link rel='stylesheet' href="css/message_style.css"> #I've a static folder
</head>
<body>

You can do both inline stylings and also insert your CSS files into your HTML from a static folder like this:
{% load static from staticfiles %} or {% load static %}
<link rel='stylesheet' href="{% static 'css/message_style.css' %}">
Note: You should load static directory before using {% static 'relative address to the file' %}, and both {% load static from staticfiles %} or {% load static %} will do the same for you but the first one is more explicit.
EDIT: If you want to send out emails and make an email template you should use inline styles and use tables, to achieve that you can check this link for more information.

Related

CSS doesn't work on pages that have variables in their url

I create a page where css works on every page except the page that is saved in the main.py file like this
#app.route('/sendKontakt/<nm>/<em>/<tx>', methods=['GET', 'POST'])
def sendKontakt(nm, em, tx):
return render_template('kontaktSucces.html', imie=nm, emile=em, wiadomosc=tx.replace("_", " "))
CSS doesn't work because url has <>.
The html code of this page looks like this:
{% extends 'base.html' %}
{% block title %}| Kontakt{% endblock %}
{% block content %}
<h1>Dziękujemy za kontakt!</h1>
<p>ImiÄ™: {{imie}}</p>
<p>Emile: {{emile}}</p>
<p>Wiadomosc: {{wiadomosc}}</p>
{% endblock %}
In base.html file header looks like
<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">
<link rel="stylesheet" href="static\css\style.css" type="text/css">
<link rel = "icon" href = "static/img/smallicon.png" type = "image/x-icon">
<title>{% block title %}{% endblock %}</title>
</head>
I apologize for not English words but they shouldn't get in the way of solving this problem.
Anyone know how to get css styles to work? I will probably not be able to write back for a few hours. Regards Kuba

Browser loads an empy css

css located in my_blog/my_blog/blog/static/blog/style.css
manage.py in my_blog/my_blog
head of html:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="{% static 'blog/style.css' %}">
<title>{% block title%} My blog {% endblock %}</title>
</head>
I'm using pycharm pro. Browser successfully load css file, but it's empty. Why this is happenning?
I don't see css loading in django console log, when pages load. I changed name of css file. And it's loaded, but once.
When I add new rule to it and reload server, browser still get old version of css.
Now it's works fine. Css loaded properly. I didn't change anything, so I don't know what did happen.
Add this to your settings.py:
os.path.join(BASE_DIR, 'static')
Then move your static folder to project's folder - /my_blog/static/blog/style.css instead of the current one.
I don't believe your href is formatted properly

Error404 while loading CSS in HTML script in Python

See the attached code: I get a 404 and there is a link to my directory.
<!DOCTYPE html>
<html>
<head>
<title>Flask App</title>
<link rel="stylesheet" href="{{url_for('static',filename='css/main.css')}}"
</head>
<body>
do you know whats wrong? see the directory here:
1: https://i.stack.imgur.com/g3Gia.jpg
I think you should move static folder to flask folder
Try this: {{ url_for('static', filename='main.css') }} thus without the css/
Do this:
<head>
<title>Flask App</title>
<link rel="stylesheet" href="/static//css/main.css"
</head>
in Flask edit this line:
this : app=Flask (__name__)
to this : app=Flask (__name__,static_url_path="/static")

django-puppeteer-pdf static files

I'm having django-puppeteer-pdf installed on my project. Page loads with css and PDF generation works great. But css files on STATIC_ROOT does not work on pdf. Any idea how to get this work?
In html template I'm having static files loaded:
{% load staticfiles %}
...
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<link href="{% static 'css/pdf-reports.min.css' %}" rel="stylesheet" />
</head>
and settings.py file:
STATIC_ROOT = os.path.join(BASE_DIR, "static_files")

Check for variable declared in child view in parent template

I am migrating several pages over to use our site's new template. These are legacy pages, though, so they need a couple extra global js files.
I was hoping to set a legacy flag in the child page views, and then have an if check in the master template, but this didn't seem to work.
Is there a better way to do this?
The ideal approach would mean I could simply declare the global legacy scripts in one place. I don't want to have to include them on every legacy child page, which is what we're doing now.
Parent template:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=.5"/>
<title>Title</title>
<link rel="stylesheet" href="/static/css/global.css"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<%block name="page_css" />
</head>
<body>
<!-- Body -->
<div class="bc-page-wrapper">
${self.body()}
</div>
<script type="text/javascript" src="/static/globals.js"></script>
% if legacy == 1:
<script type="text/javascript" src="/static/js/legacy.js"></script>
% endif
</body>
</html>
Legacy Page Inheriting Template:
<%
legacy = true
%>
<%inherit file="/global/ko_admin_template.html" />
<div class="legacy-container">
content here
</div>

Categories