views:

42

answers:

3

I'm using django.views.generic.list_detail.object_detail.

According to the documentation the view takes the variable object_id. To do this I added the following to my urlconf:

(r'^(?P<object_id>\d+)$', list_detail.object_detail, article_info),

The above line is in a separate urlconf that is included in the main urlconf.

If I leave the '^' character at the beginning of the pattern and then attempt to go to the address:

.../?object_id=1

It does not work. If I remove the '^' character the address:

.../?object_id=1

Still does not work. However if I use:

.../object_id=1 (without the question mark)

The view accepts the object_id variable and works without a problem. I have two questions about this.

First: Can the '^' character in an included urlconf be used to restrict the pattern to only match the base url pattern plus the exact string bettween a ^$ in the included urlconf?

Second: Why does the question mark character stop the view from receiving the 'object_id' variable? I thought the '?' was used to designate GET variables in a URL.

thanks

+2  A: 

That urlconf tells Django to map URLs of the kind .../1, .../123 to the given view (... being the prefix of that urlconf). (?P<object_id>\d+) tells django to assign the value captured by \d+ to the variable object_id. See also Python's documentation on regular expressions and django's documentation on its URL dispatcher.

piquadrat
+2  A: 

I'll tackle your second question first. The ? character in this context is used to denote a named group in the regular expression. This is a custom extension to regular expressions provided by Python. (See the howto for examples)

To pass an object_id append it to the URL (in your case). Like this: ../foo/app/3 where 3 is the object_id.

Manoj Govindan
A: 

first, the "r" before a string means it is a regular expression and ^ means the start of the string, $ means the end of string. in python when you put (?P<'something>a_regular_expression) python will find your matched expression for a_regular_expression in that string and return it as a variable with name: something. here \d+ means numbers, and it will find a number and pass it to the function you assigned there ( article_info ) by the name of object_id.

second, you shouldn't worry for GET urls, you just set the main url and django will manage GET variables itself. for example if you have (r'^/post/$, my_app.views.show_post) in your url patterns and you send this get request ../post/?id=10, django will use your my_app.views.show_post function and you can access the get variables in request.GET, here if you want to get id you can use this request.GET[id].

Mehran