Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django redirect doesn't reach the root url

Looks like redirect(<url>) is adding <url> to the current view url, ignoring urls.py, with respect to the Code below redirecting to "<mydomain>.com/savePersonalEdits/account/" while what I wanted is: "<mydomain>.com/account/"

Code:

At the end of some Django view function I have:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

return redirect('account/')

urls:

path('account/', views.accountSettings),
path('savePersonalEdits/', views.saveEdits, name="savePersonalEdits") #name needed for HTML form action url

>Solution :

you can use HttpResopnseRedirect for this

from django.http import HttpResponseRedirect    


return HttpResponseRedirect('/account/')

also there is a better way to redirect, create name your path in ulrs.py

path('account/', views.accountSettings, name="account_url_name"),

#and in your views import reverse and HttpResponseRedirect
from django.urls import reverse
from django.http import HttpResponseRedirect

return HttpResponseRedirect(reverse("account_url_name")) 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading