OiO.lk Blog javascript Flask cannot be found js file although static path is given
javascript

Flask cannot be found js file although static path is given


I use a setup.py in my project:

from setuptools import find_packages, setup

with open("modules/requirements.txt", "r", encoding="utf-8") as file:
    requirements = file.read().splitlines()

setup(
    name="test-app",
    version="0.1",
    description="A test app",
    packages=find_packages(where="modules"),
    package_dir={"": "modules"},
    license="MIT",
    install_requires=requirements,
    include_package_data=True
)

MANIFEST.in:

recursive-include modules *

My test route with Blueprint (I registered the Blueprint in my main.py)

current_path: str = os.path.dirname(os.path.abspath(__file__))

test = Blueprint(
   name="test", 
   import_name=__name__,
   template_folder=os.path.join(current_path, "static", "templates"),
   static_folder=os.path.join(current_path, "static")
)

The loading of the template is working without any errors:

@test.route("/test", methods=["GET", "POST"])
def test():
    return render_template("test.html")

If I now specify the path of the JavaScript file in my test.html, I get this error:
GET http://127.0.0.1:5000/static/js/app.js net::ERR_ABORTED 404 (NOT FOUND)

My test.html code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>...</title>
</head>
<body>
    <div class="container">
        <!-- ... -->
    </div>
    <script src="{{ url_for('test.static', filename="js/app.js") }}"></script>
</body>
</html>



You need to sign in to view this answers

Exit mobile version