用烧瓶服务 HTML+CSS+JS(angular)
Serving HTML+CSS+JS(angular) with flask
我想做的是,将 HTML+css+js 文件作为静态页面发送到某些路由上,例如:
@app.route('/', methods=[GET])
def index():
return <html+css+js>
我特别想远离模板,并依靠 ajax/websocket 连接到 flask 应用程序的其他路由来获取 JSON 对象并更新网页。我也很难链接 css 和 html 中的 js 文件。 url_for
方法似乎完全依赖模板系统,在我的情况下似乎无法正常工作。
例如
Directory Structure
- 重定向服务器(应用主文件夹)
- 静态
- index.html
- main.js
- venv(python3 virtualenv)
- main.py(烧瓶应用程序)
main.py
from flask import Flask, redirect
from flask import render_template
app = Flask(__name__)
@app.route('/')
def index():
return redirect("/abc/xyz")
@app.route('/abc/xyz')
def abc():
return app.send_static_file("index.html")
app.run(debug=True)
index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
<script type="text/javascript" src="{{ url_for('static', filename='main.js') }}"></script>
</head>
<body>
<h1>Welcome!</h1>
</body>
</html>
The error I get is the following
127.0.0.1 - - [28/Oct/2015 14:07:02] "GET /abc/%7B%7B%20url_for('static',%20filename='main.js')%20%7D%7D HTTP/1.1" 404 -
The HTML is returned fine but it cannot find the js
file
您将模板作为 静态 文件发送。
app.send_static_file("index.html")
我无法在不依赖模板的情况下找到它。
The following worked for me
重组我的目录如下
- 重定向服务器
- 静态
- main.js
- 模板
- index.html
- main.py
main.py
from flask import Flask, redirect
from flask import render_template
app = Flask(__name__)
@app.route('/')
def index():
return redirect("/abc/xyz")
@app.route('/abc/xyz')
def abc():
return render_template("index.html")
app.run(debug=True)
index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
<script type="text/javascript" src="{{ url_for('static', filename='main.js') }}"></script>
</head>
<body>
<h1>Welcome!</h1>
</body>
</html>
我想做的是,将 HTML+css+js 文件作为静态页面发送到某些路由上,例如:
@app.route('/', methods=[GET])
def index():
return <html+css+js>
我特别想远离模板,并依靠 ajax/websocket 连接到 flask 应用程序的其他路由来获取 JSON 对象并更新网页。我也很难链接 css 和 html 中的 js 文件。 url_for
方法似乎完全依赖模板系统,在我的情况下似乎无法正常工作。
例如
Directory Structure
- 重定向服务器(应用主文件夹)
- 静态
- index.html
- main.js
- venv(python3 virtualenv)
- main.py(烧瓶应用程序)
main.py
from flask import Flask, redirect
from flask import render_template
app = Flask(__name__)
@app.route('/')
def index():
return redirect("/abc/xyz")
@app.route('/abc/xyz')
def abc():
return app.send_static_file("index.html")
app.run(debug=True)
index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
<script type="text/javascript" src="{{ url_for('static', filename='main.js') }}"></script>
</head>
<body>
<h1>Welcome!</h1>
</body>
</html>
The error I get is the following
127.0.0.1 - - [28/Oct/2015 14:07:02] "GET /abc/%7B%7B%20url_for('static',%20filename='main.js')%20%7D%7D HTTP/1.1" 404 -
The HTML is returned fine but it cannot find the
js
file
您将模板作为 静态 文件发送。
app.send_static_file("index.html")
我无法在不依赖模板的情况下找到它。
The following worked for me
重组我的目录如下
- 重定向服务器
- 静态
- main.js
- 模板
- index.html
- main.py
- 静态
main.py
from flask import Flask, redirect
from flask import render_template
app = Flask(__name__)
@app.route('/')
def index():
return redirect("/abc/xyz")
@app.route('/abc/xyz')
def abc():
return render_template("index.html")
app.run(debug=True)
index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
<script type="text/javascript" src="{{ url_for('static', filename='main.js') }}"></script>
</head>
<body>
<h1>Welcome!</h1>
</body>
</html>