Answer the question
In order to leave comments, you need to log in
How to redirect back to the page from which you left after authorization?
I googled for a long time, but I only found an 11-year-old article on Stackoverflow, and I didn’t understand much, to be honest.
There is a link in the navigation bar on the site to log in. This link is on every page.
Please tell me how to make it so that after clicking on this link and subsequent authorization, the user is redirected back to the page from which he came when he clicked on the link to log in.
The only thing I realized is that you need to somehow use django.contrib.auth.views.login
next. From the documentation, too, little is clear.
Answer the question
In order to leave comments, you need to log in
So the GET parameter next is usually passed? More details here
UPD: Oh, I also thought about the referer, it can also come in handy.
back_url = request.get["next"] or request.META["HTTP_REFERER"] or request.path
# ещё можешь добавить reverse("index")
return redirect(back_url)
Read here MANDATORY, I recommend it just in case.
And so the simplest example:
And just look at how people did: return redirect("https://www.djangoproject.com")
Example from doc:
from django.http import HttpResponse
from django.template import loader
def my_view(request):
# View code here...
t = loader.get_template('myapp/index.html')
c = {'foo': 'bar'}
return HttpResponse(t.render(c, request), content_type='application/xhtml+xml')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question