Home > OS >  Issue with Django redirect to previously visited page
Issue with Django redirect to previously visited page

Time:03-29

I am trying to update the login view. For now, if someone will try to enter http://127.0.0.1:8000/articles/ then the user is redirected to http://127.0.0.1:8000/ (login page) and after successful log, it is redirected to http://127.0.0.1:8000/home.

What I want is to redirect users to the page they tried to visit before logging in. So if someone will try to visit http://127.0.0.1:8000/articles then after log in the website should redirect him to the articles page, not to home.

This is what I tried:

views.py

class UpdatedLoginView(LoginView):
    form_class = LoginForm
    template_name = 'user/login.html'
    redirect_field_name='main/homepage.html'
    
    def post(self, request):
        form = self.form_class(request.POST)
        if form.is_valid():
            if 'next' in request.POST:
                return redirect(request.POST['next'])

login.html

<form method="post"  novalidate>
    {% csrf_token %}
    {{ form|crispy }}
    {% if request.GET.next %}
    <input type="hidden" name="next" value="{{request.GET.next}}" />
    {% endif %}
    <button type="submit" >Login</button>
    <a href="{% url 'login' %}?next={{request.get_full_path|urlencode}}">Login test</a> // this is just a test element, I want to have button in my form
</form>

My question is how to modify UpdatedLoginView and form in order to redirect users to previously visited page. The code I tried is taken from here.

CodePudding user response:

I used the normal View instead of LoginView. You can check my code down.

class SignIn(View):
    def get(self, request, *args, **kwargs):
        next_url = request.GET.get('next')
        form = SignInForm()
        return render(request,'accounts/login.html', {"form":form, "next":next_url})

    def post(self, request, *args, **kwargs):
        form = SignInForm(request.POST)
        if form.is_valid():
            if request.method == 'POST':
                email = request.POST.get('email')
                password =request.POST.get('password')
                
                user = authenticate(request, email=email, password=password)
                
                if user is not None :
                    login(request, user)
                    next_url = request.POST.get('next')
                    if next_url != "None":
                        return HttpResponseRedirect(next_url)
                    return redirect('home')
                else:
                    messages.info(request, 'Username or password is incorrect')

            
        return render(request, 'home/index.html', {"form":form})


And I just add the hidden input field in my template:

<input type="hidden" name="next" id="next" value="{{ next }}">

CodePudding user response:

I believe HTTP_REFERER can be used to get the previous page's redirect link. Try using

referer = request.META.get("HTTP_REFERER")

to store the url of the previous page as a variable. Then when the user successfully logs in, you can call return HttpResponseRedirect(referer) instead of return redirect(request.POST['next']). Hopefully this helps with your issue.

  • Related