为python程序获取500个未定义的索引页面

时间:2014-03-19 04:00:58

标签: python web bottle

当我复制并关注此应用时,我只是在浏览Python教程

http://www.mongodb.com/presentations/building-web-applications-mongodb-introduction

文件结构如下:

./app
 ├── views/
 │     ├── index.tpl
 ├── index.py
 └── guestbookDAO.py

index.py

import bottle
import pymongo
import guestbookDAO

#This is the default route, our index page.  Here we need to read the documents from MongoDB.
@bottle.route('/')
def guestbook_index():
    mynames_list = guestbook.find_names()
    return bottle.template('index', dict(mynames = mynames_list))

#We will post new entries to this route so we can insert them into MongoDB
@bottle.route('/newguest', method='POST')
def insert_newguest():
    name = bottle.request.forms.get("name")
    email = bottle.request.forms.get("email")
    guestbook.insert_name(name,email)
    bottle.redirect('/')


#This is to setup the connection

#First, setup a connection string. My server is running on this computer so localhost is OK
connection_string = "mongodb://localhost"
#Next, let PyMongo know about the MongoDB connection we want to use.  PyMongo will manage the connection pool
connection = pymongo.MongoClient(connection_string)
#Now we want to set a context to the names database we created using the mongo interactive shell
database = connection.names
#Finally, let out data access object class we built which acts as our data layer know about this
guestbook = guestbookDAO.GuestbookDAO(database)

bottle.debug(True)
bottle.run(host='localhost', port=8082) 

guestbookDAO.py

import string

class GuestbookDAO(object):

#Initialize our DAO class with the database and set the MongoDB collection we want to use
    def __init__(self, database):
        self.db = database
        self.mynames = database.mynames

#This function will handle the finding of names
    def find_names(self):
        l = []
        for each_name in self.mynames.find():
            l.append({'name':each_name['name'], 'email':each_name['email']})

        return l

#This function will handle the insertion of names
    def insert_name(self,newname,newemail):
        newname = {'name':newname,'email':newemail}
        self.mynames.insert(newname)

在index.tpl

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to MongoDB</title>
    <style type="text/css">
        body{font-family:sans-serif;color:#4f494f;}
        form input {border-radius: 7.5px;}
        h5{display: inline;}
        .label{text-align: right}
        .guestbook{float:left;padding-top:10px;}
        .name{width:100%;float:left;padding-top: 20px}
    </style>
</head>
<body>

<div class="wrapper">
    <h1>Welcome To MongoDB!</h1>
    <div class="guestbook_input">
        <form method="post" class="form" action="/newguest">
            Name: <input type="text" name="name"/>
            Email: <input type="text" name="email"/>
            <input type="submit" value="Add Guest"/>
        </form>
    </div>

    <div class="guestbook">
        <h3>Guests:</h3>
        %for name in mynames:
            <div class="name">
                <h5>Name:</h5> {{name['name']}},
                <h5>Email:</h5> {{name['email]']}}
            </div>
        %end
    </div>
</div>




</body>
</html>

对于导致此问题的路由或模板定义,我做错了什么?

3 个答案:

答案 0 :(得分:3)

您应该将模板位置的绝对路径添加到TEMPLATE_PATH

bottle.TEMPLATE_PATH.insert(0,'/absolut/path/to/your/templates/')

找不到瓶子模板(FAQ):

  

在./和./views/中搜索模板。在mod_python或   mod_wsgi环境,工作目录(./)取决于你的   Apache设置。

所以将index.py更改为:

import os
# Add these lines before `bottle.run` line.
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
template_path = os.path.join(BASE_DIR, 'views')
bottle.TEMPLATE_PATH.insert(0, template_path)
...
...
bottle.debug(True)
bottle.run(host='localhost', port=8082) 

注意:如果您从项目的根目录运行python index.py

mongoguestbook$ ls -la
 guestbookDAO.py
 guestbookDAO.pyc
 index.py
 README.md
 views
mongoguestbook$ python index.py

然后您无需将上述代码添加到index.py,因为bottle.TEMPLATE_PATH中描述的默认值FAQ为:

print(bottle.TEMPLATE_PATH)
# ['./', './views/'])

但是如果你添加它们,那么你可以从根$ python index.py或从任何地方运行它:$ python ~/workspace/so/mongoguestbook/index.py

答案 1 :(得分:2)

模板中有拼写错误。替换:

<h5>Email:</h5> {{name['email]']}}

使用:

<h5>Email:</h5> {{name['email']}}

希望有所帮助。

答案 2 :(得分:2)

尝试在同一目录中运行index.py,例如,运行python index.py,不要运行python /same/directory/index.py

这个问题发生在我身上