簡體   English   中英

即使在創建“靜態”目錄並更改了<link>和<script> tags in the HTML file

[英]Failure to load CSS and Javascript even after creating the “static” directory and changing the <link> and <script> tags in the HTML file

目前正在嘗試學習 Flask,我正在嘗試路由到一個 HTML 文件,該文件從樣式表加載一些 CSS,從 .js 文件加載一些 Javacript。 HTML、CSS 和 JS 文件來自我之前處理過的前端項目,因此我最初只是將這些文件復制到目錄中。 當失敗時,我做了一些研究,發現我需要創建一個靜態文件夾並更改鏈接到樣式表和腳本的方式。 但是,這仍然不起作用。

燒瓶片段:

@app.route('/landing')
def landingPage():
    return render_template("index.html")

HTML 片段 (index.html):

<head>
  ...
  <link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles.css') }}">
  <script src="{{ url_for('static',filename='app.js') }}" defer> </script>
</head>

根目錄包含 app.py(Flask 文件),子目錄模板包含 index.html,子目錄 static 包含樣式.css 和 app.js。

在控制台中,由於無法加載源為 127.0.0.1:5000/static/app.js 的腳本,我收到錯誤消息。 有趣的是,該文件被稱為landing.html 而不是index.html,但我認為這與我的問題無關(如果我錯了,請告訴我)。

先感謝您,

靜態目錄必須包含在根目錄中,而不是包含在模板目錄中。

|-- app.py
|-- templates 
|   `--teamplate.html
`-- static
    |-- css
    |   `-- style.css
    `-- js
        `-- file.css

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM