1

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!!

1 Answer 1

4

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

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

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.