Search code examples
pythonhtmlflaskjinja2

Flask site can't get static files after deploy


I developed a website using flask and in my computer everthing runs ok using localhost. But when i deploy the code it returns 404 to every static file. The server codes is basic like this excluding what don't makes difference:

    from flask import Flask, render_template, request, jsonify, redirect, url_for, session
    from flask_session import Session
    from flask_cors import CORS
    
    from db import database
    
    
    app = Flask(__name__, template_folder='templates', static_folder='static')
    app.config['SECRET_KEY'] = 'secret'  # Substitua por uma chave segura em produção
    app.config['SESSION_TYPE'] = 'filesystem'
    Session(app)
    
    CORS(app)
    
    # Chamando a função para criar o banco de dados e as tabelas
    database.create_database()
    database.create_admin_user()
    database.create_default_keys()
    
    keys = database.get_keys()[1]
    
    @app.route('/')
    def index():
        if 'logged_in' in session and session['logged_in']:
            credits = database.get_user_credit(session['username'])
            free_roll = database.is_free_roll(session['username'])
            session['credits'] = credits
        else:
            free_roll = False
            credits = 0
        return render_template('index.html', translate=keys['translate'], credits=credits, free_roll=free_roll)

if __name__ == '__main__':
    app.run(debug=True)

and in my html i call the statics file like this:

<link rel="stylesheet" href="{{ url_for('static', filename='css/roulette_style.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/header.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/footer.css') }}">

follows a image of the folders structure:

enter image description here

and another image with the files

enter image description here

I'm pretty sure the code is correct cause when running in localhost no problem show up, and i more than double check if the code is similar and the folder structures aswell. So, if someone could help I would be very grateful.


Solution

  • Ok, so I'm using cpanel to host the application, and in cpanel you have the public_html folder, inside this folder there is a .htaccess file. It's an ocult dot file, so make sure to make it visible in settings. In my case I open up this file, edit it, letting be just the content of python from the file, I mean, there was a bunch of stuff about wordpress, and then the website works just fine.