gpt4 book ai didi

python - 为python程序获取500个 undefined index 页

转载 作者:行者123 更新时间:2023-11-28 20:45:47 24 4
gpt4 key购买 nike

当我复制并关注这个应用程序时,我只是在浏览 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>

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

最佳答案

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

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

未找到 Bottle 模板(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)

注意:如果您从项目的根目录运行 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.

关于python - 为python程序获取500个 undefined index 页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22495945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com