3

I am currently trying to run some tests on a RESTful web service and use the django test client to test the following get request using Client.get:

'/api/browse=ia?filter=General' 

These are my urls:

(r'^api/browse=([\w\s]+)$', 'webservice_browse_nofilter')
(r'^api/browse=([\w\s]+)\?filter=(\w+)$', 'webservice_browse')

The problem is that the wrong function is called. In this case I want to call the second function but instead the first is called. The problem is that the ? should act as a separator of the arguments but gets matched by the first pattern which still sends the correct argument 'ia' to the function instead of the whole string. I feel like I am missing something but I do not know what. The intent is to call the second function with arguments 'ia' and 'General'.

Steinin
  • 541
  • 7
  • 20

3 Answers3

1

These patterns are technically the same URL, just with different query strings.

Did you think about consolidating them to a single view, and testing to see if request.GET.get('filter') returns anything?

I don't think reversing them will do anything -- I believe Django ignores the query string when it processes the URL

Brett Thomas
  • 1,694
  • 3
  • 15
  • 21
  • They are already consolidated... sort of. the browse_nofilter function simply calls browse with appropriate arguments. According to the function, it's not detecting the second argument and is given as input to the function as an empty string which signals the function to disregard filter altogether. 'General' here is not equal to 'All'. – Steinin Nov 29 '11 at 01:05
1

See Django and query string parameters

As Brett Thomas says, you will have the query parameters available in request.GET

Community
  • 1
  • 1
Ian Clelland
  • 43,011
  • 8
  • 86
  • 87
1

To clarify what the other answerers have said, everything after the ? is not part of the URL, and will not be matched in the regex.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895