【发布时间】:2015-03-01 04:00:36
【问题描述】:
我的 python 烧瓶应用程序中有三个文件,这些文件是基于 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 烧瓶服务器,相同的前端应用程序会正常运行。 另外,我通过测试警报检查了 html 文件是否正确引用了 app.js 文件。
【问题讨论】:
-
flask.render_template("index.html")你也需要传递参数 -
对于您在模板中使用的 store.product.name
-
我是烧瓶和角度的新手。您能否明确告诉我哪个文件中的哪一行需要从什么更改为什么?如果您这样做,请将其作为答案发布。也会帮助其他人。
标签: javascript python angularjs flask