noben
noben

Reputation: 561

Django Redirect URL

I am new to Django, and i am now trying to use the HttpResponseRedirect() function. But I am so confused that if I use it like HttpResponseRedirect('good/'), and the current page is '/bad/', it can only be redirected to '/bad/good/', which is an url of current page appended with the url value from the HttpResponseRedirect() function. I tried to search google, and could not find any solution.

How can I redirect to the page with specific url? For example, HttpResponseRedirect('/good/') to /good/ rather than /bad/good/ ?

Upvotes: 1

Views: 1815

Answers (2)

Nicolas Brugneaux
Nicolas Brugneaux

Reputation: 353

If you want to redirect to urls in your domain, you can use redirect. You can use redirect to view with by using patterns in your urls.py file or to any urls you 'd like. Although I strongly encourage the usage of views as indicates the different tutorials available on their website.

Better check out django documentation that is one of the most complete out there (to my humble opinion)

/edit for lack of clarity indeed.

Upvotes: -1

Daniel Roseman
Daniel Roseman

Reputation: 600006

Surely you must see that there's a difference between 'good/' and '/good/'? The former will always add itself onto the existing page, whereas the latter will start from the root. This is basic web behaviour, and nothing to do with Django.

In any case, you should never hard-code URLs like that, but should use Django's URL-reversing functionality to calculate the URLs dynamically.

Upvotes: 3

Related Questions