Why does Django keep redirecting to `/dashboard/` instead of /profile/dashboard or a custom URL even after changing the path?

I'm working on a Django project with separate apps for authentication (auth_app) and user dashboard (dashboard_app). After logging in successfully, I expect the user to be redirected to dashboard, but Django keeps redirecting to /dashboard/ instead of /profile/dashboard/ or any other URL I configure.

Here's what I've done:

  1. URL Configuration:

    • In dashboard_app/urls.py:

      from django.urls import path
      from .views import DashboardController
      
      app_name = 'dashboard_app'
      
      urlpatterns = [
          path("dashboard/", DashboardController.as_view(), name='dashboard'),
      ]
      
    • In project/urls.py, I have included the dashboard_app under the /profile/ path:

      from django.contrib import admin
      from django.urls import path, include
      
      urlpatterns = [
          path('admin/', admin.site.urls),
          path('auth/', include('auth_app.urls')),
          path('profile/', include('dashboard_app.urls')),
      ]
      
  2. Login Controller:

    After a successful login, I use reverse to redirect to the dashboard:

    from django.shortcuts import redirect
    from django.urls import reverse
    
    class LoginController(View):
        def post(self, request):
            # Authenticate the user
            ...
            return redirect(reverse('dashboard_app:dashboard'))
    

The Problem:

After logging in successfully, I am redirected to /dashboard/ instead of /profile/dashboard/ or any other custom path. Even if I try to change the URL in dashboard_app/urls.py:

from django.urls import path
from .views import DashboardController

app_name = 'dashboard_app'

urlpatterns = [
    path('somewhere/', DashboardController.as_view(), name='dashboard'),
]

however it keeps redirecting to /dashboard/ instead of profile/somewhere/.

  • I don’t have any custom middlewares.
  • I don’t use JavaScript; only Jinja for templating.

The HTML form used in the register_page.html template looks like this:

<div id="error-message" style="{% if form.errors %}display:block{% else %}display:none{% endif %};">
    <ul>
        {% for field in form %}
        {% for error in field.errors %}
        <li>{{ error }}</li>
        {% endfor %}
        {% endfor %}
    </ul>
</div>

<!-- Registration Form -->
<form id="register-form" method="POST" action="{% url 'auth_app:register' %}">
    {% csrf_token %}

    <!-- Username -->
    {{ form.username.label_tag }}
    {{ form.username }}<br>

    <!-- Password1 -->
    {{ form.password1.label_tag }}
    {{ form.password1 }}<br>

    <!-- Password2 -->
    {{ form.password2.label_tag }}
    {{ form.password2 }}<br>

    <button type="submit">Register</button>
</form>

<p>Already have an account? <a href="{% url 'auth_app:login' %}">Login here</a></p>
</body>
</html>

What I've Tried:

  • I checked the URL patterns in both dashboard_app/urls.py and project/urls.py to ensure they're correct.
  • I tried modifying the redirect URL in the LoginController, but the redirection always ends up being /profile/dashboard/.
  • I cleared the Django cache and restarted the development server, but the issue persists.

Question:

Why is Django redirecting to /dashboard/ instead of the expected /profile/dashboard/ or any other custom path? How can I fix this issue?

Вернуться на верх