Home > front end >  How to prevent redirect_to_login from targeting LOGIN_REDIRECT_URL
How to prevent redirect_to_login from targeting LOGIN_REDIRECT_URL

Time:03-17

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 ><a href="/" style="margin: 0"><img src="{% static 'images/logo_negative_text.svg' %}" ></a></div>

    <div >

        <!-- form headline -->
        <h1 >Login to your Farena Account</h1>
        <!-- form -->
        <form  method="post" action=".">
            {% csrf_token %}
            <div >
                <div >{{ form.non_field_errors }}</div>
            </div>
            <div >
                <div >{{ form.username.label }}*</div>
                <div >{{ form.username }}</div>
            </div>
            <div >
                <div >{{ form.password.label }}*</div>
                <div >{{ form.password }}</div>
                <a href="/password_change/"><div >Forgot Password?</div></a>
            </div>
            <!-- submit button -->
            <button  type="submit">Log In</button>
        </form>
    </div>

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

{% endblock form %}

CodePudding user response:

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.

  • Related