Home > OS >  Django redirect doesn't reach the root url
Django redirect doesn't reach the root url

Time:05-31

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:

return redirect('account/')

urls:

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

CodePudding user response:

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")) 

CodePudding user response:

Just do it

return redirect('/account/')

or

return redirect('account')
  • Related