我试图通过这个非常简单的例子来了解金字塔遍历。我还没有完全掌握的是从数据库“注入”Article
对象的位置。
实际上,/Article
正确地找到并呈现article_view
,但这是相当无用的。如何/何时/何地使用URL的下一部分从db查询特定文章?例如。 /Article/5048230b2485d614ecec341d
。
任何线索都会很棒!
初始化的.py
from pyramid.config import Configurator
from pyramid.events import subscriber
from pyramid.events import NewRequest
import pymongo
from otk.resources import Root
def main(global_config, **settings):
""" This function returns a WSGI application.
"""
config = Configurator(settings=settings, root_factory=Root)
config.add_static_view('static', 'otk:static')
# MongoDB
def add_mongo_db(event):
settings = event.request.registry.settings
url = settings['mongodb.url']
db_name = settings['mongodb.db_name']
db = settings['mongodb_conn'][db_name]
event.request.db = db
db_uri = settings['mongodb.url']
MongoDB = pymongo.Connection
if 'pyramid_debugtoolbar' in set(settings.values()):
class MongoDB(pymongo.Connection):
def __html__(self):
return 'MongoDB: <b>{}></b>'.format(self)
conn = MongoDB(db_uri)
config.registry.settings['mongodb_conn'] = conn
config.add_subscriber(add_mongo_db, NewRequest)
config.include('pyramid_jinja2')
config.include('pyramid_debugtoolbar')
config.scan('otk')
return config.make_wsgi_app()
resources.py
class Root(object):
__name__ = None
__parent__ = None
def __init__(self, request):
self.request = request
def __getitem__(self, key):
if key == 'Article':
return Article(self.request)
else:
raise KeyError
class Article:
__name__ = ''
__parent__ = Root
def __init__(self, request):
self.reqeust = request
# so I guess in here I need to update the Article with
# with the document I get from the db. How?
def __getitem__(self, key):
raise KeyError
views.py
from pyramid.view import view_config
from otk.resources import *
from pyramid.response import Response
@view_config(context=Root, renderer='templates/index.jinja2')
def index(request):
return {'project':'OTK'}
@view_config(context=Article, renderer='templates/view/article.jinja2')
def article_view(context, request):
# I end up with an instance of Article here as the context.. but
# at the moment, the Article is empty
return {}
答案 0 :(得分:8)
您通常会从网址遍历的 id 部分返回Article
个对象。
遍历的结果是,对于URL路径中的每个元素,都会查找一个对象,并为 next 路径元素查找创建新的当前对象。
因此,对于Article
,要求根对象提供与该名称匹配的内容,并将该查找的结果设为新的“当前”对象,并5048230b2485d614ecec341d
然后查看那个新对象。
所以,你要找的是一个调度程序对象,它根据你传递的id越长,查找文章, 返回你的Article
个实例:
class Root(object):
__name__ = None
__parent__ = None
def __init__(self, request):
self.request = request
def __getitem__(self, key):
if key == 'articles':
dispatch = ArticleDispatcher(self.request)
dispatch.__name__ = key
dispatch.__parent__ = self
return dispatch
raise KeyError(key)
class ArticleDispatcher(object):
__name__ = None
__parent__ = None
def __init__(self, request):
self.request = request
def __getitem__(self, key):
# Get a hold of the database here:
db = findDatabase(self.request)
if db.exists(key):
data = db.load(key)
art = Article(data)
art.__name__ = key
art.__parent__ = self
return art
raise KeyError(key)
class Article:
__name__ = None
__parent__ = None
def __init__(self, data):
self.data = data
请注意我在使用ArticleDispatcher
网址路径时返回/articles
的方式,以及我如何设置__name__
和__parent__
变量;您需要能够为这些实例生成URL。
现在返回的Article
对象包含实际的文章数据,视图在渲染时可以访问该信息。
你真的想去研究Pyramid Traversal tutorial,这可以更详细地解释这一点。