简体   繁体   中英

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. Reverse for 'index' not found. 'index' is not a valid view function or pattern name.

Any help appreciated.

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),
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM