'How to prevent redirect_to_login from targeting LOGIN_REDIRECT_URL

So in general when a user logs in, my app redirects him to dashboard-overview:

# settings.py

# Login and Logout target routes
LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = 'dashboard-overview'
LOGOUT_REDIRECT_URL = '/'

However for a specific case I want the user to be redirected to 'calculator-page' using a helper function which doesn't work as the user is again redirected to the value of LOGIN_REDIRECT_URL.

# views.py

def render_calculator(request, slug):
    """
    Renders the calculator page
    """
    # Check for the user being authenticated
    if not request.user.is_authenticated:
        return redirect_to_login('calculator-page')
   
        # routes to http://127.0.0.1:8000/login/?next=calculator-page which looks fine, 
        # but then it fails after login to re-route as specified and sends to
        # 'dashboard-overview' again

    else:

        club_obj = Offer.objects.get(club__slug=slug)

        # Create Context
        context = {
            'club_obj': club_obj,
        }

        return render(request, 'core/calculator.html', context)
# urls.py 

from django.urls import path, include
from django.contrib.auth import views
from applications.user import views as user_views

urlpatterns = [

    # Login View
    path('login/', views.LoginView.as_view(template_name='user/login.html'), name="login"),
    path('logout/', views.LogoutView.as_view(), name="logout"),

    # Sign Up
    path('signup/', user_views.signup, name='signup'),

]
# template user/login.html

{% extends 'user/user_frame.html' %}
{% load static %}

<!-- Styles -->
{% block styles %}
    <link href="{% static 'css/user_frame.css' %}" rel="stylesheet" type="text/css">
{% endblock styles %}


{% block form %}

    <div class="logo-container"><a href="/" style="margin: 0"><img src="{% static 'images/logo_negative_text.svg' %}" class="logo-image"></a></div>

    <div class="form-container">

        <!-- form headline -->
        <h1 class="form-headline">Login to your Farena Account</h1>
        <!-- form -->
        <form class="form" method="post" action=".">
            {% csrf_token %}
            <div class="error-wrapper">
                <div class="errors">{{ form.non_field_errors }}</div>
            </div>
            <div class="email-wrapper field-wrapper">
                <div class="tag name-tag">{{ form.username.label }}*</div>
                <div class="input">{{ form.username }}</div>
            </div>
            <div class="password-wrapper field-wrapper">
                <div class="tag">{{ form.password.label }}*</div>
                <div class="input">{{ form.password }}</div>
                <a href="/password_change/"><div class="forgot-password">Forgot Password?</div></a>
            </div>
            <!-- submit button -->
            <button class="login-button" type="submit">Log In</button>
        </form>
    </div>

    <a href="/signup"><div class="account-info">No account yet? Sign Up!</div></a>

{% endblock form %}


Solution 1:[1]

The issue is with your template for the login view - you're missing the next form field that tells Django where to redirect the user after they have logged in. See the documentation for sample template code for this view.

Specifically, you need to add this inside your form:

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

That's missing right now which means Django will always just default to LOGIN_REDIRECT_URL.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 solarissmoke