Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyramid route matching and query parameters

Tags:

python

pyramid

I have a Pyramid web service, and code samples are as follows:

View declaration:

@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')

Routing:

config.add_route('services/Prices/GetByTicker/', 'services/Prices/GetByTicker/{ticker}/{startDate}/{endDate}/{period}')

Now I know this is all screwed up but I don't know what the convention is for Pyramid. At the moment this works inasmuch as the request gets routed to the view successfully, but then I get a "Dictionary object not callable" exception.

The URL looks horrible:

@root/services/Prices/GetByTicker/ticker=APPL/startDate=19981212/endDate=20121231/period=d

Ideally I would like to be able to use a URL something like:

@root/services/Prices/GetByTicker/?ticker=APPL&startDate=19981212&endDate=20121231&period=d

Any Pyramid bods out there willing to take five minutes to explain what I'm doing wrong?

like image 228
Mel Padden Avatar asked Feb 06 '13 10:02

Mel Padden


1 Answers

from you sample code, i think you use the URL Dispatch

so it should be like this

config.add_route('services/Prices/GetByTicker/', 'services/Prices/GetByTicker/')

then the URL like:
@root/services/Prices/GetByTicker/?ticker=APPL&startDate=19981212&endDate=20121231&period=d
will match it

--edit--
you don't have to use a name like "services/Prices/GetByTicker" for route_name,and you can get the GET params use request.params['key']
View declaration:

@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']

Routing:

config.add_route('services_Prices_GetByTicker', 'services/Prices/GetByTicker/')
like image 85
Paul Yin Avatar answered Sep 27 '22 20:09

Paul Yin