Forums

Default URL not applying css to .html

Hi, so I set up some html and css for my web app but when I access it with the default username.pythonanywhere.com, I just get the index.html file returned which isn't really a problem (but I am interested in how I could change which HTML is displayed in that case). The problem is that the CSS isn't getting applied to it, but when I access the same index.html through any other URL I get it returned with the CSS so the problem shouldn't be that some path is wrong.

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    return render_template('index.html')

This is the catch_all I am using and as I said it seems to work for any URL apart from <code>username.pythonanywhere.com</code> and <code>username.pythonanywhere.com/</code> . What could be a cause of this problem?

Hi there, could you show us how you include your CSS in the index.html file?

Hi, here is the head of my index.html file with the CSS include.

<!DOCTYPE HTML>
<HTML>
<head>
    <link rel="stylesheet" href="{{ url_for('static',filename='css/main.css') }}">
    <title>Welcome</title>
</head>

It looks like a static files mapping issue -- see this help page.

This post didn't really help me. While I got it to work using the href="/static/css/main.css" I now have the problem that the redirect on a form on this site doesn't work as it should. I get a Method not allowed error and this in the log

File "/home/BenS/mysite/templates/index.html", line 4, in top-level template code
    <link rel="stylesheet" href="{{ url_for('static/',filename='css/main.css') }}">
...
werkzeug.routing.BuildError: Could not build url for endpoint 'static/' with values ['filename']. Did you mean 'static' instead?

This confuses me because that line isn't even in the file anymore.

Did you try 'static' instead of 'static/'?