【发布时间】:2015-03-17 13:19:58
【问题描述】:
我正在使用 Python 和 Angular JS 开发一个 Web 应用程序。我正在为我的网络服务器使用瓶子 python 框架。
我的文件如下:
app.py
import json
import os
from bottle import run, route, template, static_file, request, redirect, get, post
import json
#import MySQLdb
#######################
# Static file servers #
#######################
ROOTPATH = ''
@route('/assets/<filepath:path>')
def serve_static(filepath):
return static_file(filepath, root='static/')
##############
# Handlers #
##############
@route('/')
def root():
return template('index.html')
def main():
run(host='0.0.0.0', port=55500, debug=True, reloader=True)
if __name__ == '__main__':
main()
index.html
<!DOCTYPE html>
<html ng-app="astra">
<head>
<title> ASTRA DB </title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js"></script>
</head>
<body ng-controller="mainCtrl">
<table>
<tr>
<th> Test Case ID </th>
<th> Test Case Description </th>
<th> Tester Assigned </th>
</tr>
<tr ng-repeat="testCase in testDB">
<td>{{ testCase.testCaseID }}</td>
<td>{{ testCase.testCaseDesc }}</td>
<td>{{ testCase.tester}}
</tr>
</table>
</body>
<script>
angular.module('astra', [])
.controller('mainCtrl', ['$scope', function($scope) {
$scope.testDB = [
{
"testCaseID" : 1,
"testCaseDesc" : "To create a test case table",
"tester" : "Veeresh A"
},
{
"testCaseID" : 2,
"testCaseDesc" : "To go to wonder la",
"tester" : "Veeresh A"
},
{
"testCaseID" : 3,
"testCaseDesc" : "Never take a Meru Cab",
"tester" : "Pallavi Bansod"
},
{
"testCaseID" : 4,
"testCaseDesc" : "To kill a mocking bird",
"tester" : "pallavi bansod"
},
{
"testCaseID" : 5,
"testCaseDesc" : "To avoid a farha khan movie",
"tester" : "Satyajit Patnaik"
},
{
"testCaseID" : 6,
"testCaseDesc" : "To talk to the customer care",
"tester" : "Mummana Santhoshi"
},{
"testCaseID" : 7,
"testCaseDesc" : "To be late to wherever you go",
"tester" : "Deepa Javoor"
}
];
}]);
</script>
</html>
从命令提示符运行python app.py 时,服务器启动,但是当我在浏览器上点击“localhost:55500”地址时,它会引发以下错误。
> $ python app.py Bottle v0.13-dev server starting up (using
> WSGIRefServer())... Listening on http://0.0.0.0:55500/ Hit Ctrl-C to
> quit.
>
> Traceback (most recent call last): File "e:\git\pychip\bottle.py",
> line 861, in _handle
> return route.call(**args) File "e:\git\pychip\bottle.py", line 1741, in wrapper
> rv = callback(*a, **ka) File "app.py", line 45, in root
> return template('index.html') File "e:\git\pychip\bottle.py", line 3494, in template
> return TEMPLATES[tplid].render(kwargs) File "e:\git\pychip\bottle.py", line 3320, in render
> self.execute(stdout, env) File "e:\git\pychip\bottle.py", line 3307, in execute
> eval(self.co, env) File "e:\git\pychip\views\index.html", line 15, in <module>
> <td>{{ testCase.testCaseID }}</td> NameError: name 'testCase' is not defined
> 127.0.0.1 - - [17/Mar/2015 09:50:53] "GET / HTTP/1.1" 500 1719
> 127.0.0.1 - - [17/Mar/2015 09:50:53] "GET /favicon.ico HTTP/1.1" 404 749
我收到名称错误异常。谁能帮我解决这个问题?
【问题讨论】:
-
您将 index.html 渲染为模板,瓶子和角度模板的语法类似于
{{ }}。 Bottle 正在寻找变量testCase,您的意思是使用 angular 来代替。将 index.html 作为静态文件提供或仅使用简单的网络服务器。 -
something是什么?你能给我一个确切的错误吗? -
@RameshRajendran 我只是使用了一些通用手段。在我的示例中,某些东西等于变量名 testCase。你可以参考上面的角码。
-
@kedar 我该怎么做?能具体点吗?
-
我对bottle不熟悉所以不能多说,但是如果你只是想提供HTML页面,没必要使用bottle。在带有 index.html 的目录中运行(从命令行)
python -m SimpleHTTPServer 8018,然后在浏览器中转到http://localhost:8018/index.html。
标签: javascript python angularjs python-2.7 bottle