【问题标题】:Trying to use Flask with Polymer. I get the error "GET /bower_components/google-map/google-map.html HTTP/1.1 404 -"尝试将 Flask 与 Polymer 一起使用。我收到错误“GET /bower_components/google-map/google-map.html HTTP/1.1 404 -”
【发布时间】:2015-12-27 10:28:35
【问题描述】:

我正在尝试将 Polymer 与 Flask 一起使用。

这里是 hello.py:

from flask import Flask
from flask import render_template
app = Flask(__name__)

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

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

这里是 main.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="/bower_components/webcomponentsjs/webcomponents.js"></script>
    <meta charset="utf-8">
    <title>title</title>
    <link rel="import" href="/bower_components/google-map/google-map.html">
</html>

我收到以下错误:“GET /bower_components/google-map/google-map.html HTTP/1.1”404 -

【问题讨论】:

    标签: python polymer polymer-1.0


    【解决方案1】:

    尝试将聚合物文件添加为static resource,像这样

    <script type="text/javascript" src="{{ url_for('static', filename='yourfile.js') }}"></script>
     <!-- similarly for link-tag too -->
    

    【讨论】:

      【解决方案2】:

      问题在于,flask 正在尝试加载一些静态内容,并且默认情况下它会在名为 static 的文件夹中查找它。如果您想从模板目录提供 html 文件,那么您仍然需要确保在 index.html 文件中的所有 href 中指定 /static/path/to/static/file。这是我制作的应用程序的示例目录结构

      MyApp/
      -- app.py
      -- static/
      ---- index.html
      ---- bower_components/
      ------ ...
      

      app.py 大概是这样的

      from flask import Flask
      app = Flask(__name__)
      
      @app.route('/')
      def get_index():
          return app.send_static_file('index.html')
      

      和 index.html

      ... 
      <link rel="import" href="/static/src/my-app.html">
      <my-app></my-app>
      ...
      

      因为 Flask 在收到 Polymer 想要加载的所有静态文件的请求时,默认会在 app 目录中查找名为 static 的文件夹。

      Flask 允许您通过将静态文件目录的名称传递给 Flask 构造函数来更改它的名称。查看详情here

      如果您想查看一些示例 code,我在 github 上发布了一个示例。它使用烧瓶和一种起始聚合物套件。

      【讨论】:

        猜你喜欢
        • 2019-02-13
        • 2016-04-11
        • 1970-01-01
        • 2022-01-26
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2019-08-24
        • 1970-01-01
        相关资源
        最近更新 更多