A
A
assadasd932018-03-05 10:54:05
Flask
assadasd93, 2018-03-05 10:54:05

Why is css and js not connected?

I am writing a website using flask_socketio. But for some reason, css and js are not connected, although everything is correct. When I run just html, everything is loaded. To render the template I use the following:

@app.route('/')
def index():
    return render_template('index.html', async_mode=socketio.async_mode)

What could be the reason?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2018-03-05
@assadasd93

The static must be in the static directory when served by Flask:

├───static
│   ├───css
│   │       style.css
│   │
│   └───js
│           script.js
│
├───templates
│       hello.html
│
│   app.py

And accordingly, the links must contain the name of the static directory: It is even better to use a special tag
<script src="{{ url_for('static', filename='js/script.js') }}"></script>

A
Astrohas, 2018-03-05
@Astrohas

you need to distribute the static somehow. Use nginx or something else for this. In the meantime, you can manually create handlers for directories by type

from flask import Flask

# set the project root directory as the static folder, you can set others.
app = Flask(__name__, static_url_path='path/to/staic/')

*****

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question