【发布时间】:2013-01-23 03:00:18
【问题描述】:
当我试图将我的 url 指向 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
views/admin.py的内容:
@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!'}
我对源代码进行了一些更改。 http://localhost:6543/admin/product 绝对有效。但是,现在http://localhost:6543/admin/product/add 似乎没有显示布局,http://localhost:6543/admin/product/YYYY/edit 也没有显示布局。
views/admin.py的内容:
@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>'}
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':
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/add 时,输出为:
< mycart.resources.ProductName object at 0x10abb7990 >
add
但是,它显示 404 错误,所以我猜我的 views/admin.py 某处是错误的?我试过切换 view_config 的顺序,但无济于事:
@view_config(context='mycart:resources.ProductName', name='add', ... )
....
@view_config(context='mycart:resources.ProductName', name='edit', ... )
至于使用以下url编辑时:http://localhost:6543/admin/vendor/50b0d01ce815af3c4167040e/edit,控制台输出为:
< mycart.resources.ProductName object at 0x10abb4bd0 >
edit
所以我知道在我的上下文中,我应该使用 context='mycart:resources.ProductName',并将名称设置为可编辑。但是,它没有在控制台中显示我在views/admin.py 中设置的编辑消息。
我哪里出错了?
【问题讨论】:
-
您的视图引用了
ProductName,但您的Product资源我正在加载SettingsName。这是复制/粘贴问题,还是您的真正问题? -
附带说明,
__parent__属性应该是一个实际的实例,而不是一个类。因此,您应该做类似p = Product(); p.__parent__ = self; return p的事情。如果你不解决这个问题,你稍后会发布另一个关于一个模糊错误的问题。 -
哦,那应该是 ProductName 而不是 SettingsName,我在这里发帖后发现了这个错误,忘记编辑帖子了。谢谢,我会再试一次。
-
会不会有一个关于遍历 + mongodb 的分步指南,教类似 localhost:6543/admin/product/{arbitrary_url} ,localhost:6543/admin/settings/{arbitrary_url等对于没有 python / 金字塔背景的人?我试过环顾四周,但大多数人都认为人们对 pylon 了解甚少。
-
所以对于parent来说,如果它应该是一个实际的实例,如果url是localhost:6543/admin/product,在产品类(resources.py)中, parent 应该 = Admin(); ,并且产品名称 parent = Product()?