我在cherrypy中有一个URL,类似于堆栈溢出:
http://sample.com/post/12345/hello-world但是,我想让以下URL在cherrypy中也起作用:
http://sample.com/post/12345/hello-world?from=something&else=123并应更正为:
http://sample.com/post/12345/hello-world我怎么能这么做?
我正在使用popargs和_cp_dispatch,但没有成功。如有任何建议,将不胜感激。
谢谢
编辑
我让它基于saaj的答案工作,但是我想将代码移到index(),urls都返回404。
import cherrypy
class App:
@cherrypy.expose
def index(self, id, name = None, **kwargs):
if kwargs:
# do your querystring processing
raise cherrypy.HTTPRedirect(cherrypy.url())
if not name:
# get name part for canonical url
name = '{0}/hello-world'.format(id)
raise cherrypy.HTTPRedirect(cherrypy.url(name))
return '{0} {1} {2}'.format(id, name, kwargs)
if __name__ == '__main__':
cherrypy.quickstart(App(), '/post', config)更多的帮助是非常感谢的。我仍然是一个快乐的初学者。
发布于 2014-11-04 11:13:18
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cherrypy
config = {
'global' : {
'server.socket_host' : '127.0.0.1',
'server.socket_port' : 8080,
'server.thread_pool' : 8
},
}
class App:
@cherrypy.expose
def index(self):
return '''
<ul>
<li>
<a href='/post/12345/hello-world'>/post/12345/hello-world</a>
</li>
<li>
<a href='/post/12345/hello-world?from=something&else=123'>
/post/12345/hello-world?from=something&else=123</a>
</li>
<li><a href='/post/12345'>/post/12345 (more like SO)</a></li>
</ul>
'''
@cherrypy.expose
def post(self, id, name = None, **kwargs):
if kwargs:
# do your querystring processing
raise cherrypy.HTTPRedirect(cherrypy.url())
if not name:
# get name part for canonical url
name = '{0}/hello-world'.format(id)
raise cherrypy.HTTPRedirect(cherrypy.url(name))
return '{0} {1} {2}'.format(id, name, kwargs)
if __name__ == '__main__':
cherrypy.quickstart(App(), '/', config)https://stackoverflow.com/questions/26727608
复制相似问题