Daniel
Daniel

Reputation: 618

Django Reverse Not Found when reversing a url in main app

I can not get my head around why this simple reverse() call for one of my Django-Views is not working. The error occurs in some of my tests:

Reverse code snippet:

# myapp/tests.py
response = self.client.get(reverse('index', args=()))

URL registry:

# myapp/urls.py
urlpatterns = [
    path('', views.index, 'index'),
    path('configuration/', include('configuration.urls')),
    path('admin/', admin.site.urls),
]

view:

#myapp/views.py
def index(request):
    elements_list = DB_ELEMENTS.objects.all()
    return render(request, "startpage.html", {'elements': elements_list})

The error I keep getting is Reverse for 'index' not found. 'index' is not a valid view function or pattern name.

Any help appreciated.

Upvotes: 0

Views: 1076

Answers (1)

Daniel Lee
Daniel Lee

Reputation: 8001

As per the docs: One needs to use the name keyword argument (kwarg):

# myapp/urls.py
urlpatterns = [
    path('', views.index, name='index'),
    path('configuration/', include('configuration.urls')),
    path('admin/', admin.site.urls),
]

Upvotes: 2

Related Questions