Reputation: 781
I created a Django project where a parent have a list of child. I wanted to show the detail of a child with url as - /parent/4/child/3. How can I define two namespaces in a single url? Can Someone explain how can i achieve this or suggest some example!!
Upvotes: 1
Views: 88
Reputation: 1964
I wanted to show the detail of a child with url as - /parent/4/child/3. How can I define two namespaces in a single url?
It seems like you want to define a URL that takes two integers (id's) and maps them to keywords.
url(r'^foo/(?P<foo_id>[0-9]+)/bar/(?P<bar_id>[0-9]+)/$', foo_bar_view),
The above url pattern will give you two different integer key, value pairs.
foo_id
as well as bar_id
.
Here's what foo_bar_view
might look like.
def foo_bar_view(request, *args, **kwargs):
foo_id = kwargs["foo_id"]
bar_id = kwargs["bar_id"]
return HttpResponse(
"foo_id: {0} -- bar_id: {1}".format(
foo_id=foo_id,
bar_id=bar_id,
)
)
If that was your view function and you made a request to a url like the following.
/foo/12/bar/34/
Then your page will receive an httpresponse of:
foo_id: 12 -- bar_id: 34
Upvotes: 4