Django does not redirect to NEXT when login is required

I am using a custom signin view, the problem is when I go to a page which have the @login_required decorator, it redirect the user to the signin page, but after the user is logged it does not redirect him to the initial page

but When I use the login view provided by Django, it redirects to the desired page after login.

urls.py

urlpatterns = [
    path("signin/", views.signin, name="signin"),
    path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
]

views.py

def signin(request):
    if request.method == "POST":
        form = UserLoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data["username"]
            password = form.cleaned_data["password"]
            #login
            user = authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    auth_login(request, user)
                else:
                    pass
            else:
                messages.add_message(request, messages.INFO, mark_safe(
                    "Sorry, your password was incorrect. Please double-check your password."))

    else:
        form = UserLoginForm()

    return render(request, "users/signin.html", {'form' : form})
Back to Top