2024-01-07

Login redirects in Django

I have a auth system with django-allauth library, and i have some login, logout functionally implemented and it works fine. But I want to implement a profile page on my own. With this I'm so confusing right with Django redirects.

I have a class based view called HomeRequestHandler that inherits a LoginRequiredMixin:

from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import TemplateView


class HomeRequestHandler(LoginRequiredMixin, TemplateView):
    template_name = "home.html"

And in my urls i just call it to "":

from django.views.generic import TemplateView
from django.urls import path
from . import views

urlpatterns = [
    path("", views.HomeRequestHandler.as_view(), name="home"),
    path("profile/", TemplateView.as_view(template_name="account/profile.html"), name="profile"),
]

In my settings.py, I have a constant for LOGIN_REDIRECT_URL:

LOGIN_REDIRECT_URL = reverse_lazy('profile')

When I try login, I am redirected to "/" even with LOGIN_REDIRECT_URL defined. But if I change one thing in my view:

from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import TemplateView


class HomeRequestHandler(LoginRequiredMixin, TemplateView):
    template_name = "home.html"
    redirect_field_name = "redirect_to"

It change my url http://localhost:8000/accounts/login/?next=/ to http://localhost:8000/accounts/login/?redirect_to=/ and now the constant works normally, why???



No comments:

Post a Comment