Reputation: 951
When I tried to point my url to http://localhost:6543/admin/product/50b0d01ce815af3c4167040e/edit
, somehow it shows a 404 error. I believe somewhere, my views is wrong, but I can't seem to find out what went wrong after numerous tries.
Contents of 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
Contents of 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!'}
I've made some changes to the source code. http://localhost:6543/admin/product
is definitely working. However, seems like now http://localhost:6543/admin/product/add
isn't showing the layout nor is http://localhost:6543/admin/product/YYYY/edit
showing the layout.
Contents of 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>'}
Contents of 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
In my console, when I point my url to http://localhost:6543/admin/product/add
, the output is:
< mycart.resources.ProductName object at 0x10abb7990 >
add
However, it is showing a 404 error, so I'm guessing my views/admin.py
is wrong somewhere? I've tried switching the order of the view_config around, but to no avail:
@view_config(context='mycart:resources.ProductName', name='add', ... )
....
@view_config(context='mycart:resources.ProductName', name='edit', ... )
As for when editing using the following url: http://localhost:6543/admin/vendor/50b0d01ce815af3c4167040e/edit
, the console output is:
< mycart.resources.ProductName object at 0x10abb4bd0 >
edit
So I know that in my context, I should use context='mycart:resources.ProductName', and the name set it edit. However, it isn't showing the edit message in the console that I have set in the views/admin.py
.
Where could I have gone wrong?
Upvotes: 2
Views: 310
Reputation: 171
If you do if key == 'somefing'
in __getitem__
it is sign that you don't need pure traversal.
Use URLdispatch or Hybrid aproach.
ADDED:localhost:6543/admin/product/add
shows not found, becoze you register 'add' view for ProductName, but from this path you get Product object, that have not view 'add' registred for it, thats why 404 not found
Upvotes: 1