我有一个金字塔网络服务,代码示例如下:
查看声明:
@view_config(route_name="services/Prices/GetByTicker/")
def GET(request):
ticker = request.GET('ticker')
startDate = request.GET('startDate')
endDate = request.GET('endDate')
period = request.GET('period')
Run Code Online (Sandbox Code Playgroud)
路由:
config.add_route('services/Prices/GetByTicker/', 'services/Prices/GetByTicker/{ticker}/{startDate}/{endDate}/{period}')
Run Code Online (Sandbox Code Playgroud)
现在我知道这一切都搞砸了,但我不知道金字塔的惯例是什么.目前这是有效的,因为请求被成功路由到视图,但后来我得到一个"字典对象不可调用"异常.
URL看起来很糟糕:
@根/服务/价格/ GetByTicker /股票= APPL /的startDate = 19981212 /结束日期= 20121231 /周期= d
理想情况下,我希望能够使用以下URL:
@根/服务/价格/ GetByTicker /?股票= APPL&的startDate = 19981212&结束日期= 20121231&周期= d
任何金字塔在那里愿意花五分钟来解释我做错了什么?
Pau*_*Yin 12
从您的示例代码,我认为您使用URL Dispatch
所以它应该是这样的
config.add_route('services/Prices/GetByTicker/', 'services/Prices/GetByTicker/')
Run Code Online (Sandbox Code Playgroud)
那么URL如:
@root/services/Price/GetByTicker /?ticker = APPL&startDate = 19981212&endDate = 20121231&period = d
将匹配它
--edit--
你不必为route_name 使用像"services/Prices/GetByTicker"这样的名字,你可以使用request.params['key']
View声明来获取GET参数:
@view_config(route_name="services_Prices_GetByTicker")
def services_Prices_GetByTicker(request):
ticker = request.params['ticker']
startDate = request.params['startDate']
endDate = request.params['endDate']
period = request.params['period']
Run Code Online (Sandbox Code Playgroud)
路由:
config.add_route('services_Prices_GetByTicker', 'services/Prices/GetByTicker/')
Run Code Online (Sandbox Code Playgroud)
查询字符串将变为request.GET字典.您正在使用括号来调用字典而不是通过括号访问项目.对于诸如此类的网址
@根/服务/价格/ GetByTicker /?股票= APPL&的startDate = 19981212&结束日期= 20121231&周期= d
request.GET['ticker'] # -> 'APPL' or an exception if not available
request.GET.get('ticker') # -> 'APPL' or None if not available
request.GET.get('ticker', 'foo') # -> 'APPL' or 'foo' if not available
request.GET.getall('ticker') # -> ['APPL'] or [] if not available
Run Code Online (Sandbox Code Playgroud)
如果您希望ticker多次提供,则最后一个选项很有用.
request.params是的组合request.GET和request.POST其中后者是代表该请求的身体的形式的上传的字典.
无论如何,答案是request.GET('ticker')语法上不是我提到的选项之一,停止这样做.:-)
| 归档时间: |
|
| 查看次数: |
7652 次 |
| 最近记录: |