NameError:name" something"未定义 - 使用Angular JS和Python的Web App

时间:2015-03-17 04:32:38

标签: javascript python angularjs python-2.7 bottle

我正在使用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时,服务器启动,但是当我点击&#34; localhost:55500&#34;在浏览器上的地址,它会引发以下错误。

> $ 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

我收到了nameerror异常。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

回答我的问题是:

而不是做以下

@route('/')
def root():
    return template('index.html')

这样做:

@route('/')
def root():
    return static_file('index.html', root="views/")

正如kedar说的那样,瓶子和棱角有类似的模板语法。因此,我需要将HTML页面用作静态文件,以便在此处遵循角度模板。