2014-03-19 45 views
2

我只是通過Python的教程看500未定義的索引頁時,我複製並遵循這個程序獲得的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> 

我在做什麼錯誤的,這是造成這個問題的路由或模板定義?

+0

我不知道瓶子什麼,但我必須假設它不是一個問題在您的模板。你有沒有可以發佈的堆棧跟蹤? –

回答

2

嘗試運行在同一目錄下,例如index.py,運行,不跑python /same/directory/index.py

這個問題發生在我

2

您在模板中有錯字。替換:

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

有:

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

希望有所幫助。

+0

謝謝,它可能是一個問題,它不影響python路由,我無法通過本地主機加載應用程序:8082 – user2167582

+0

@ user2167582你得到的錯誤頁是什麼? – alecxe

+0

錯誤:500內部服務器錯誤 對不起,請求的URL'http:// localhost:8082 /'導致錯誤: 找不到模板'index'。 – user2167582

3

您應該添加模板位置的絕對路徑TEMPLATE_PATH

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

瓶模板未找到(FAQ):

Bottle searches in ./ and ./views/ for templates. In a mod_python or mod_wsgi environment, the working directory (./) depends on your Apache settings.

因此改變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) 

注意:如果您運行從項目的根:

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

那麼你不需要上面的代碼添加到index.py,因爲descibed在FAQbottle.TEMPLATE_PATH默認值爲:

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

但是,如果你添加他們,那麼你可以從根$ python index.py或從任何地方運行它:$ python ~/workspace/so/mongoguestbook/index.py

+0

我應該在bottle.route上添加bottle.TEMPLATE_PATH.insert(0,'/ path/to/app/views')嗎? – user2167582

+0

@ user2167582,我的答案已更新。 –