1

I know django.conf.urls.defaults.url extracts regular expression to get object-id or others.

I wonder if I can do the same thing in my view.

Suppose I have a url rule such as url(r'^/path1/path2/(?P<my_object_id>\d+)/path3, my_view, name='my_view_name')
In a view, I have a string that matches the url such as /path1/path2/34/path3, how can I get the 34 from the string?

ie, Is there a function which takes view_name(or the same url regex in urls.py), and a url string and returns positional/keyword arguments as the url() does it?

foo(view_name, url_string): 
  ... 
  return (args, kwargs)

2 Answers 2

1

You can use django.core.urlresolvers.resolve.

Sign up to request clarification or add additional context in comments.

Comments

0

You should be able to reference 34 using my_object_id.

Here's the relevant text from the django tutorial polls app:

The poll_id='34' part comes from (?P\d+). Using parentheses around a pattern “captures” the text matched by that pattern and sends it as an argument to the view function; ?P defines the name that will be used to identify the matched pattern; and \d+ is a regular expression to match a sequence of digits (i.e., a number).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.