SethuNagaKarthik
SethuNagaKarthik

Reputation: 409

Django V2.1 reverse is not a valid view function or pattern name

I am very new to Django and I have created two apps in my project one is login & server_status. I have used the namespace method to access my login and server_status across both apps. It is working fine when I tried to access index but when I tried to access dashboard in server_status it is not working.

Reverse for 'server_status' not found. 'server_status' is not a valid view function or pattern name

# server_status URL

from django.urls import path
from . import views

    urlpatterns = [
path('', include('server_status.urls', namespace='server_status')),
path('', include('login.urls', namespace='login')),
path('admin/', admin.site.urls),
]

# login app URL

app_name = 'login'

urlpatterns = [
    path('login', views.index, name='index')
]


# project URL

    from django.contrib import admin
    from django.urls import include, path

    app_name = 'server_status'

urlpatterns = [
    path('', views.index, name='index'),
    path('dashboard/', views.dashboard, name='dashboard'),
    path('server_status/', views.server_status, name='server_status'),
    path('request_access/', views.request_access, name='request_access'),
]

In server_status/templates/server_status/index.html

<a href="{% url 'server_status:dashboard' %}" class="button primary centered-s">Login</a>

I know this is very simple one, but it makes me very complicated.

Upvotes: 1

Views: 1803

Answers (2)

shacker
shacker

Reputation: 15371

In the posted code, you define urlpatterns twice, so the first set of urlpatterns is overridden by the second. Since server_status is defined in the first set, it is not present in memory, since you obliterate it in the second definition. index is the only pattern that survives. I think what you meant to do in the second stanza was to add to the urlpatterns with:

urlpatterns += [...]

Upvotes: 2

Diego Puente
Diego Puente

Reputation: 2004

To declare namespace you should do something like this:

# project URL

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('', include('server_status.urls', namespace='server_status')),
    path('', include('login.urls', namespace='login')),
    path('admin/', admin.site.urls),
]

then you can use

<a href="{% url 'login:index' %}" class="button primary centered-s">Login</a>

or

<a href="{% url 'server_status:index' %}" class="button primary centered-s">Server status index</a>

Upvotes: 0

Related Questions