2

I have problem with rendering html / css.

main:

from quart import Quart, render_template, redirect, url_for
app = Quart(__name__)

@app.route("/")
async def index():
    return await render_template("index.html")

app.run(debug=True)

index.html:

<!doctype html>
<html lang="en">
<head>
    <link rel="stylesheet" href="index/style.css">
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Some title</title>
</head>
<body>
    <div class="container">
        <img src="index/some_image.png" alt="something" style="width:50%;">
        <a href="/login">
            <button class="button button_Login">Login</button>
        </a>
    </div>
</body>
</html>

style.css:

.container {
  position: relative;
  text-align: center;
}

.container .button_Login {
    position: absolute;
    bottom: 1%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: black;
    border: 3px solid #eeccff;
    color: white;
    padding: 15px 32px;
    text-align: center;
    border-radius: 4px;
    font-size: 20px;
    transition-duration: 0.4s
}

.container .button_Login:hover {
    color: black;
    background-color: #eeccff;
}

.button_Login:active {
    position: relative;
    top: 2px;
}

body {
    background: black;
}

file structure:

app
  templates
    index
      some_image.png
      style.css
    index.html
  main.py

When I look on preview in PyCharm, everything works just fine; but when I try to run the code, website shows css and png files exist; but there is nothing inside of them.

Chrome was showing me three errors:

Failed to load resource: the server responded with a status of 404 () style.css:1

Failed to load resource: the server responded with a status of 404 () Amethyst.png:1

Failed to load resource: the server responded with a status of 404 () style.css:1

1 Answers1

2

Static files should be placed in a static directory, docs. So for your example the file structure should be,

app
  static
    some_image.png
    style.css
  templates
    index.html
  main.py

Then in the index.html file you can use url_for to link to them, i.e.

<!doctype html>
<html lang="en">
<head>
    <link rel="stylesheet" href="{ url_for('static', filename='style.css') }}">
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Amethyst Dashboard</title>
</head>
<body>
    <div class="container">
        <img src="{ url_for('static', filename='some_image.png') }}" alt="something" style="width:50%;">
        <a href="/login">
            <button class="button button_Login">Login</button>
        </a>
    </div>
</body>
</html>

Note that the linked docs are for Flask, but the same API and rules apply for Quart.

pgjones
  • 6,044
  • 1
  • 14
  • 12