@view_config(route_name='short_url')
def url_expander(request):
short_url = request.matchdict['short_url']
# verify short_url is a valid format you define before doing a db query
# your db query code to find long_url
# SELECT long_url FROM t WHERE short_url=short_url
# handle case of no match in db
return HTTPFound(location=long_url)
您可以配置单个路由。第一个参数是路由的名称,第二个参数是用于匹配请求的模式。你知道吗
然后添加一个视图来处理请求,获取
short_url
的matchdict值,在数据库中查找,然后返回重定向响应。你知道吗参见文档https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/urldispatch.html#routing-examples和https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/views.html#using-a-view-callable-to-do-an-http-redirect中的示例。你知道吗
相关问题 更多 >
编程相关推荐