Python 金字塔和遍历的问题
当我试图将我的url指向Python 金字塔和遍历的问题,python,pyramid,traversal,Python,Pyramid,Traversal,当我试图将我的url指向http://localhost:6543/admin/product/50b0d01ce815af3c4167040e/edit,不知怎的,它显示了404错误。我相信在某个地方,我的观点是错误的,但经过无数次的尝试,我似乎无法找出哪里出了问题 resources.py的内容: from pyramid.security import Authenticated from pyramid.security import Allow from pyr
http://localhost:6543/admin/product/50b0d01ce815af3c4167040e/edit
,不知怎的,它显示了404错误。我相信在某个地方,我的观点是错误的,但经过无数次的尝试,我似乎无法找出哪里出了问题
resources.py的内容
:
from pyramid.security import Authenticated
from pyramid.security import Allow
from pyramid.response import Response
class Root(object):
__name__ = ''
__parent__ = None
def __init__(self, request):
pass
def __getitem__(self, key):
if key == 'admin_login':
return Admin()
elif key == 'admin':
return Admin()
raise KeyError
class Admin(object):
__name__ = ''
__parent__ = Root
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self):
pass
def __getitem__(self, key):
if key == 'product':
print ('admin: ' , key)
return Product()
if key == 'category':
print ('admin: ' + key)
return Category()
raise KeyError
class Product(object):
__name__ = ''
__parent__ = Admin
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self):
pass
def __getitem__(self, key):
if key :
return ProductName(key)
print ('Approaching KeyError: ', key)
raise KeyError
class ProductName(object):
__parent__ = Product
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self, _str):
self.__name__ = _str;
self.__parent__ = Settings;
print ('ProductName: ' + _str)
pass
@view_config(context='mycart:resources.Product', renderer='post.jinja2')
@view_config(context='mycart:resources.ProductName', name='edit', renderer='admin/settings/commissions.jinja2', permission = 'admin')
print (' in product edit? ')
return {'msg': 'yay editing!'}
@view_config(context='mycart:resources.ProductName', name='edit', renderer='admin/product/test.jinja2', permission = 'admin')
def product_edit(context, request):
print 'edit here?'
return { 'msg': '<div class="alert alert-success">Product Edit!</div>'}
@view_config(context='mycart:resources.ProductName', name='add', renderer='admin/product/test.jinja2', permission = 'admin')
def product_add(context, request):
print 'add in here?'
return { 'msg': '<div class="alert alert-success">Product Add</div>'}
@view_config(context='mycart:resources.ProductName', name="add" , request_method="POST", renderer='admin/product/add.jinja2', permission = 'admin')
def product_add_post(context, request):
return { 'msg': '<div class="alert alert-success">Product Added Successfully!</div>'}
@view_config(context='mycart:resources.Product', name='', renderer='admin/product/list.jinja2', permission = 'admin')
def product_list(context, request):
return { 'msg': '<div class="alert alert-success">Listing of products</div>'}
from pyramid.security import Authenticated
from pyramid.security import Allow
from pyramid.response import Response
class Root(object):
__name__ = __parent__ = None
def __init__(self, request):
pass
def __getitem__(self, key):
if key == 'admin_login':
return Admin()
elif key == 'admin':
return Admin()
raise KeyError
class Admin(object):
__name__ = ''
__parent__ = Root
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self):
pass
def __getitem__(self, key):
if key == 'product':
return Product()
#if key == 'category':
# return Category()
raise KeyError
class Product(object):
__name__ = ''
__parent__ = Admin
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self):
print ('Product() self _name: ' , self.__name__, ' parent: ', self.__parent__)
pass
def __getitem__(self, key):
print ('product: ' , key)
if key:
print ('key is true: ' , key)
return ProductName(key)
raise KeyError
class ProductName(object):
__name__ = ''
__acl__ = [
( Allow, Authenticated, 'admin')
]
def __init__(self, _key):
p = Product()
p.__name__ = _key
p.__parent__ = self
print ( 'ProductName() init: ', _key)
print ( p.__parent__)
print ( p.__name__)
print ('\n\n')
pass
def __getitem__(self, _key):
print ( 'ProductName() __get__item key: ', _key)
if _key == 'edit':
p = Product()
p.__name__ = _key
p.__parent__ = self
print ('ProductName()->edit parent: ')
print ( p.__parent__)
print ( p.__name__)
print ('\n\n')
return p
raise KeyError
视图/admin.py的内容
:
from pyramid.security import Authenticated
from pyramid.security import Allow
from pyramid.response import Response
class Root(object):
__name__ = ''
__parent__ = None
def __init__(self, request):
pass
def __getitem__(self, key):
if key == 'admin_login':
return Admin()
elif key == 'admin':
return Admin()
raise KeyError
class Admin(object):
__name__ = ''
__parent__ = Root
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self):
pass
def __getitem__(self, key):
if key == 'product':
print ('admin: ' , key)
return Product()
if key == 'category':
print ('admin: ' + key)
return Category()
raise KeyError
class Product(object):
__name__ = ''
__parent__ = Admin
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self):
pass
def __getitem__(self, key):
if key :
return ProductName(key)
print ('Approaching KeyError: ', key)
raise KeyError
class ProductName(object):
__parent__ = Product
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self, _str):
self.__name__ = _str;
self.__parent__ = Settings;
print ('ProductName: ' + _str)
pass
@view_config(context='mycart:resources.Product', renderer='post.jinja2')
@view_config(context='mycart:resources.ProductName', name='edit', renderer='admin/settings/commissions.jinja2', permission = 'admin')
print (' in product edit? ')
return {'msg': 'yay editing!'}
@view_config(context='mycart:resources.ProductName', name='edit', renderer='admin/product/test.jinja2', permission = 'admin')
def product_edit(context, request):
print 'edit here?'
return { 'msg': '<div class="alert alert-success">Product Edit!</div>'}
@view_config(context='mycart:resources.ProductName', name='add', renderer='admin/product/test.jinja2', permission = 'admin')
def product_add(context, request):
print 'add in here?'
return { 'msg': '<div class="alert alert-success">Product Add</div>'}
@view_config(context='mycart:resources.ProductName', name="add" , request_method="POST", renderer='admin/product/add.jinja2', permission = 'admin')
def product_add_post(context, request):
return { 'msg': '<div class="alert alert-success">Product Added Successfully!</div>'}
@view_config(context='mycart:resources.Product', name='', renderer='admin/product/list.jinja2', permission = 'admin')
def product_list(context, request):
return { 'msg': '<div class="alert alert-success">Listing of products</div>'}
from pyramid.security import Authenticated
from pyramid.security import Allow
from pyramid.response import Response
class Root(object):
__name__ = __parent__ = None
def __init__(self, request):
pass
def __getitem__(self, key):
if key == 'admin_login':
return Admin()
elif key == 'admin':
return Admin()
raise KeyError
class Admin(object):
__name__ = ''
__parent__ = Root
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self):
pass
def __getitem__(self, key):
if key == 'product':
return Product()
#if key == 'category':
# return Category()
raise KeyError
class Product(object):
__name__ = ''
__parent__ = Admin
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self):
print ('Product() self _name: ' , self.__name__, ' parent: ', self.__parent__)
pass
def __getitem__(self, key):
print ('product: ' , key)
if key:
print ('key is true: ' , key)
return ProductName(key)
raise KeyError
class ProductName(object):
__name__ = ''
__acl__ = [
( Allow, Authenticated, 'admin')
]
def __init__(self, _key):
p = Product()
p.__name__ = _key
p.__parent__ = self
print ( 'ProductName() init: ', _key)
print ( p.__parent__)
print ( p.__name__)
print ('\n\n')
pass
def __getitem__(self, _key):
print ( 'ProductName() __get__item key: ', _key)
if _key == 'edit':
p = Product()
p.__name__ = _key
p.__parent__ = self
print ('ProductName()->edit parent: ')
print ( p.__parent__)
print ( p.__name__)
print ('\n\n')
return p
raise KeyError
我对源代码做了一些修改<代码>http://localhost:6543/admin/product肯定在工作。然而,现在似乎
http://localhost:6543/admin/product/add
未显示布局,也未显示http://localhost:6543/admin/product/YYYY/edit
显示布局
视图/admin.py的内容
:
from pyramid.security import Authenticated
from pyramid.security import Allow
from pyramid.response import Response
class Root(object):
__name__ = ''
__parent__ = None
def __init__(self, request):
pass
def __getitem__(self, key):
if key == 'admin_login':
return Admin()
elif key == 'admin':
return Admin()
raise KeyError
class Admin(object):
__name__ = ''
__parent__ = Root
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self):
pass
def __getitem__(self, key):
if key == 'product':
print ('admin: ' , key)
return Product()
if key == 'category':
print ('admin: ' + key)
return Category()
raise KeyError
class Product(object):
__name__ = ''
__parent__ = Admin
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self):
pass
def __getitem__(self, key):
if key :
return ProductName(key)
print ('Approaching KeyError: ', key)
raise KeyError
class ProductName(object):
__parent__ = Product
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self, _str):
self.__name__ = _str;
self.__parent__ = Settings;
print ('ProductName: ' + _str)
pass
@view_config(context='mycart:resources.Product', renderer='post.jinja2')
@view_config(context='mycart:resources.ProductName', name='edit', renderer='admin/settings/commissions.jinja2', permission = 'admin')
print (' in product edit? ')
return {'msg': 'yay editing!'}
@view_config(context='mycart:resources.ProductName', name='edit', renderer='admin/product/test.jinja2', permission = 'admin')
def product_edit(context, request):
print 'edit here?'
return { 'msg': '<div class="alert alert-success">Product Edit!</div>'}
@view_config(context='mycart:resources.ProductName', name='add', renderer='admin/product/test.jinja2', permission = 'admin')
def product_add(context, request):
print 'add in here?'
return { 'msg': '<div class="alert alert-success">Product Add</div>'}
@view_config(context='mycart:resources.ProductName', name="add" , request_method="POST", renderer='admin/product/add.jinja2', permission = 'admin')
def product_add_post(context, request):
return { 'msg': '<div class="alert alert-success">Product Added Successfully!</div>'}
@view_config(context='mycart:resources.Product', name='', renderer='admin/product/list.jinja2', permission = 'admin')
def product_list(context, request):
return { 'msg': '<div class="alert alert-success">Listing of products</div>'}
from pyramid.security import Authenticated
from pyramid.security import Allow
from pyramid.response import Response
class Root(object):
__name__ = __parent__ = None
def __init__(self, request):
pass
def __getitem__(self, key):
if key == 'admin_login':
return Admin()
elif key == 'admin':
return Admin()
raise KeyError
class Admin(object):
__name__ = ''
__parent__ = Root
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self):
pass
def __getitem__(self, key):
if key == 'product':
return Product()
#if key == 'category':
# return Category()
raise KeyError
class Product(object):
__name__ = ''
__parent__ = Admin
__acl__ = [
(Allow, Authenticated, 'admin')
]
def __init__(self):
print ('Product() self _name: ' , self.__name__, ' parent: ', self.__parent__)
pass
def __getitem__(self, key):
print ('product: ' , key)
if key:
print ('key is true: ' , key)
return ProductName(key)
raise KeyError
class ProductName(object):
__name__ = ''
__acl__ = [
( Allow, Authenticated, 'admin')
]
def __init__(self, _key):
p = Product()
p.__name__ = _key
p.__parent__ = self
print ( 'ProductName() init: ', _key)
print ( p.__parent__)
print ( p.__name__)
print ('\n\n')
pass
def __getitem__(self, _key):
print ( 'ProductName() __get__item key: ', _key)
if _key == 'edit':
p = Product()
p.__name__ = _key
p.__parent__ = self
print ('ProductName()->edit parent: ')
print ( p.__parent__)
print ( p.__name__)
print ('\n\n')
return p
raise KeyError
在我的控制台中,当我将url指向http://localhost:6543/admin/product/add
,输出为:
< mycart.resources.ProductName object at 0x10abb7990 >
add
< mycart.resources.ProductName object at 0x10abb4bd0 >
edit
至于使用以下url进行编辑:http://localhost:6543/admin/vendor/50b0d01ce815af3c4167040e/edit
,控制台输出为:
< mycart.resources.ProductName object at 0x10abb7990 >
add
< mycart.resources.ProductName object at 0x10abb4bd0 >
edit
编辑
因此,我知道在我的上下文中,我应该使用context='mycart:resources.ProductName',并将其名称设置为edit。但是,它没有在控制台中显示我在视图/admin.py
中设置的编辑消息
我哪里会出错 如果在
\uu getitem\uuu
中执行If key==“somefing”
,则表明不需要纯遍历。
使用URLdispatch或混合aproach
添加:
localhost:6543/admin/product/add
显示未找到,因为您注册了ProductName的“添加”视图,但从该路径可以获得尚未注册“添加”视图的产品对象,这就是为什么404找不到您的视图引用了ProductName
,而您的产品
资源我正在加载设置名称
。这是复制/粘贴问题,还是您真正的问题?请注意,\uuuu parent\uuuu
属性应该是实际的实例,而不是类。因此,您应该执行类似于p=Product();p、 _u_父母_;=自己;返回p
。如果你不解决这个问题,你将在稍后发布另一个关于一个模糊错误的问题。哦,那应该是一个ProductName而不是SettingsName,我在这里发布后注意到了这个错误,忘记了编辑帖子。谢谢,我要再试一次。会不会有一个关于traversal+mongodb的dummy分步指南,教给那些没有python/pyramid背景的人一些像localhost:6543/admin/product/{arbitral_url},localhost:6543/admin/settings/{arbitral_url}之类的东西?我试着四处看看,但大多数人都认为人们对pylon的了解很少。因此,对于父级,如果它应该是一个实际实例,如果url是localhost:6543/admin/product,在产品类(resources.py)中,父级应该=admin(),和productname parent=Product()?但这是否解释了视图无法工作的原因?这纯粹是一个练习,让我了解遍历是如何工作的,即使终端显示了相关的消息shm。。。在终端控制台中,我得到了add,因此我认为上下文是ProductName而不是Product。尽管如此,我已经将上下文更改为Product,并添加了名称,但是没有,它仍然显示404关于所提出的第二个问题,关于0x10abb4bd0处的