<分区>
我的 python flask 应用程序中有三个文件,该应用程序基于前端的 angular js 构建。
app.py
import json
import flask
import numpy as np
app = flask.Flask(__name__)
@app.route("/")
def index():
return flask.render_template("index.html")
if __name__ == "__main__":
import os
port = 80
# Open a web browser pointing at the app.
os.system("open http://localhost:{0}".format(port))
# Set up the development server on port 80.
app.debug = True
app.run(port=port)
index.html
<!DOCTYPE html>
<html ng-app="gemStore">
<head>
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js'></script>
<script type="text/javascript" src="static/app.js"></script>
</head>
<body ng-controller="StoreController as store">
<div class="list-group-item">
<h3>{{store.product.name}} <em class="pull-right">25</em></h3>
</div>
</body>
</html>
app.js
(function() {
var app = angular.module('gemStore', []);
app.controller('StoreController', function(){
this.product = gem;
}
);
var gem = {
name: "Chicken",
price: 123.11,
color: "White"
};
})();
当我运行此应用程序时,出现以下行会导致错误:
<h3>{{store.product.name}} <em class="pull-right">25</em></h3>
我收到一条错误消息。 jinja2.exceptions.UndefinedError UndefinedError: 'store' 未定义
这很奇怪,因为即使我不使用 python flask 服务器,同样的前端应用程序也能正常运行。 此外,我通过测试警报检查了 html 文件是否正确引用了 app.js 文件。