javascript - 将 CSS 链接到 flask 时出错

标签 javascript python html css flask

我试图将我编写的一些 CSS 链接到单页应用程序,但是当我通过 CMDER 运行它时,它一直给我一个 404 错误。

我的应用

webapp.py

静态文件夹

--index.html

--CSS ---主题.css

--图像 --- hudson 1.jpg

模板

--newyork.html

127.0.0.1 - - [28/Nov/2016 10:08:10] "GET /%7B%7B%20url_for('static',%20filename='css/theme.css')%20%7D%7D HTTP/1.1" 404

这是我得到的错误。 这是我的代码。

Index.html

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#newyorkbutton").click(function(){
$.get("/newyork", function(data, status){
$("#city").html(data);
});
});
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>A Blog ABout My Travels</title>
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static', filename='css/theme.css') }}">
</head>

<body>

    <div id="mainPicture">
        <div class="picture">
            <div id="headerTitle">Places I've Been</div>
            <div id="headerSubtext">A Blog About My Travels</div>
        </div>
    </div>
    <div id="page">
        <input type="submit" name="NewYork" id="newyorkbutton" value="New York">
</div>
<div id="city" class="contentBox">
        <div class="innerBox">
            <h2>Welcome to My Travels</h2>
          <div class="contentText"><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum..</p><br />
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum..</p></div>        
</div>
</div>

</body>
</html>

这是我的 newyork.html:

<!doctype html>
<head>
<title>New York</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/theme.css') }}">
</head>
<div class="innerBox">
<h2>New York</h2>       
<div class="contentText"><p>
    New York is a state in the northeastern United States, and is the 27th-most extensive, fourth-most populous, and seventh-most densely populated U.S. state. New York is bordered by New Jersey and Pennsylvania to the south and Connecticut, Massachusetts, and Vermont to the east. The state has a maritime border in the Atlantic Ocean with Rhode Island, east of Long Island, as well as an international border with the Canadian provinces of Quebec to the north and Ontario to the west and north. 
</p>
<img src="{{ url_for('static', filename='images/newyork1.jpg') }}" alt="New York" style="width:750px;height:350px;">
<p>
    The state of New York, with an estimated 19.8 million residents in 2015, is often referred to as New York State to distinguish it from New York City, the state's most populous city and its economic hub. With an estimated population of 8.55 million in 2015, New York City is the most populous city in the United States and the premier gateway for legal immigration to the United States. The New York City Metropolitan Area is one of the most populous urban agglomerations in the world. New York City is a global city, exerting a significant impact upon commerce, finance, media, art, fashion, research, technology, education, and entertainment, its fast pace defining the term New York minute. 
</p></div> 
</div> 

还有我的 webapp.py:

from flask import Flask, render_template, request

app = Flask(__name__)  

@app.route("/")
def root():
    return app.send_static_file('index.html')

@app.route("/newyork") 
def newyork(): 
    return render_template('newyork.html')

if __name__ == "__main__":     
    app.run()

最佳答案

问题是您将 index.html 作为静态文件发送,而不是作为呈现的模板发送。 首先将 index.htmlstatic 文件夹移动到 templates 文件夹,

然后, 你应该改变

@app.route("/")
def root():
    return app.send_static_file('index.html')

webapp.py 中 到:

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

它会起作用。

关于javascript - 将 CSS 链接到 flask 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40841984/

相关文章:

javascript - jQuery - 悬停不适用于 for 循环

javascript - 如何在 JavaScript 中使用 goto?

javascript - Google 自动完成地点 HTML5 模式

python - 在 bash 中运行并行命令

python - 您是否可以仅修改 XML 文件中的文本字符串并仍然保持 .docx 封装的完整性和功能?

javascript - 占位符的替代品,以便在 Internet Explorer 中工作

html - 开关按钮上的不同颜色

php - 通过 jQuery 使用 AJAX 发布 MySQL 结果

python - Django : Cannot import modules

html - 如何在CSS中显示随机句子