views:

159

answers:

2

In django, when a URL is matched, the match group is passed as a second parameter to the view function, the first being a HttpRequest object. For example, with a URL patter like this

'/foo/(\d{2})/', 'app.views.handler'

the handler routine will have

def handler(request, value):

where value will contain a two digit number (as a string).

My question is: is value also contained in the request object, and if yes, how can I get it (of course, parsing the URL from the request object is not an option, too impractical).

Thanks

A: 

Can you give any reason why you would need this?

I don't see why parsing the url path is 'impractical', given that you've already got a regexp that works, in your urlconf.

Daniel Roseman
because then I would duplicate the regexp.
Stefano Borini
But you seem to want to duplicate something anyway, since the parameters are already being passed to your view function and you want them somewhere else as well. You still haven't given a use case.
Daniel Roseman
because I want to pass only the request object around as a single entity, not the request object _and_ the list of parameters as two separate entities.
Stefano Borini
There's no particular use case. I can manage also by passing stuff around (request, parameter_list). However, if I had the parameters already in the request, I could have obtained them from the request itself, instead of carrying around duplicate info (both in the parameter list and in the request object).
Stefano Borini
A: 

I'm not going to debate the merit of your idea. Just try to answer your question:

No there is no way, other than applying the regex to the URL again, to get at the url parameter.

Your view will be the first point where the parameter list will be available. Why don't you just create a wrapper object to encapsulate your request and your parameter list at that point?

Just pass that around...

celopes