Alex Luis Arias
Alex Luis Arias

Reputation: 1394

Django: Search URL for keyword RegExp

I have the following URL:
http://127.0.0.1:8000/databaseadd/q=DQ-TDOXRTA?recipe=&recipe=&recipe=&recipe=&recipe=

I'd like to match if the keyword "recipe=" is found in the url.
I have the following line in django url.py but it's not working:
url(r'recipe=', 'plots.views.add_recipe'),

Are the "&" and the "?" throwing it off?

Thanks!
Alex

Upvotes: 0

Views: 164

Answers (2)

Igonato
Igonato

Reputation: 10787

Part after the ? not used in the url dispatching process because it contains GET parameters. Otherwise you will not be able to use a GET request with parameters on patterns like r'^foo/&' (ampersand at the end means the end of the string, and without it it would be harder to use patterns like r'^foo/' and r'^foo/bar/' at the same time).

So for your url the pattern should look like r'^databaseadd/q=DQ-TDOXRTA&' and then in add_recipe view you need to check for the recipe GET parameter.

Upvotes: 2

Idriss Neumann
Idriss Neumann

Reputation: 3838

I'd like to match if the keyword "recipe=" is found in the url

You could try something like :

import re;
re.match (r'.*recipe=.*', "http://127.0.0.1:8000/databaseadd/q=DQ-TDOXRTA?recipe=&recipe=&recipe=&recipe=&recipe=")

Upvotes: 0

Related Questions