-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 03/11/2014 10:49 AM, Ron Drongowski wrote:
> We are creating a traversal based pyramid-app using our ZCA-based
> components we designed for our CMS. So for a regular article an url
> would look like this: /path/to/my_article. Traversal ends and we can
> register a default view (name=''). This is easy. But, since we have a
> lot of SEOs around us, we have very specific needs for custom-urls. We
> were asked ro realize the scheme /path/to/my_article/page-2 for the
> second page of an article. This would lead to an dynamic view_name,
> which is currently not possible - even in an hybrid application, if I
> understood the documentation right. I assume you could solve this
> problem by either registering a lot of views, extend the traversal
> (implement an __getitem__ for the article) or implement your own
> request factory. All of which do not seem to be the right thing, while
> a regex view-name seems to be th eright thing. Is there a solution for
> this?
You need 'request.subpath'[2]::
@view_config(context=MyModel, renderer='templates/mytemplate.pt')
def my_view(request):
end = request.subpath[-1:]
if end and end[0].startswith('page-'):
page = int(end[0].split('-')[1])
else:
page = None
pages = []
url = request.resource_url(request.context)
for i in range(10):
page_url = '%spage-%d' % (url, i)
pages.append((i, page_url))
return {'project': 'ugh',
'page': page,
'pages': pages,
}
This would match for URLs such as '/@@/page-1' on the "ZODB starter" app.
If you want to avoid the '@@' element in the visible path, you can add
it via a new-request subscriber[2]::
from pyramid.events import NewRequest
from pyramid.events import subscriber
@subscriber(NewRequest)
def mysubscriber(event):
r = event.request
last = r.path_info.rsplit('/', 1)[1]
if last.startswith('page-'):
page = last.split('-')[1]
r.path_info = r.path_info[:-7] + '@@' + '/page-%s' % page
[1]
http://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request.subpath
[2]
http://docs.pylonsproject.org/projects/pyramid/en/latest/api/events.html#pyramid.events.subscriber
Tres.
- --
===================================================================
Tres Seaver +1 540-429-0999 [email protected]
Palladion Software "Excellence by Design" http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iEYEARECAAYFAlMfluQACgkQ+gerLs4ltQ73gwCg3Gn44Z9BuFnGdLTZzUr8q+y8
o1QAoMI2oW0jlzYRgtyl8Gs8pjMUKMjP
=gIOO
-----END PGP SIGNATURE-----
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.