Django Project Redirecting to /accounts/login/ URL Despite Custom URLs Configuration
I’m working on a Django project and encountering an issue where the login functionality is redirecting to URLs like /accounts/login/ or /accounts/profile/ which I did not configure.
I have customized the login and registration views and removed the default Django authentication URLs. However, the server seems to be redirecting to these default paths.
Here are the details of the problem:
Project Setup:
Django version: 5.1 Custom views for login, registration, and logout are implemented. The urls.py in my app does not include the default Django authentication URLs. urls.py in the app (firstapp/urls.py):
python
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path("", views.index, name="index"),
path("index/", views.index, name="index"),
path("error/", views.custom_404, name="error"),
path("login/", views.login, name="login"),
path("register/", views.register, name="register"),
path("profile/", views.profile, name="profile"),
path("logout/", views.logout, name="logout"),
# Remove or comment out the line below
# path("accounts/", include("django.contrib.auth.urls")),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# Remove or comment out handler404 if not using custom 404 view
# handler404 = views.custom_404
Views File (views.py):
python
from django.shortcuts import render, redirect
from django.contrib.auth import login as auth_login, logout as auth_logout
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User, auth
from .models import Customer
from django.contrib import messages
from .forms import CustomerUpdateForm, CustomerPasswordChangeForm
Views code...
Issue Description: Despite configuring custom URLs and removing the inclusion of Django's default authentication URLs, when attempting to log in, the application redirects to /accounts/login/ and /accounts/profile/, which results in a 404 error as shown in the screenshot enter image description here.
Question:
Why is my Django project redirecting to /accounts/login/ and /accounts/profile/ URLs, and how can I ensure it uses only the custom URLs I've defined? Are there any other settings or configurations I might be missing that could be causing this issue?
this is a screenshot of the error! enter image description here
Thank you in advance for your assistance!
I tryed to search for the problem and I have searched on ChatGPT and Claude AI and I have tryed to add the url in my path but it didn't worked
I think it will be helpful for you:
You need to decorate your views using login_not_required
from django.contrib.auth.decorators import login_not_required
@login_not_required
def public_view(request):
return HttpResponse("This is a public view accessible without login.")
# Using Class Based Views
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView
@method_decorator(login_not_required, name='dispatch')
class PublicView(TemplateView):
...
FOr more info read docs here