简体   繁体   English

在主应用程序中反转 url 时找不到 Django Reverse

[英]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.我不明白为什么这个简单的 reverse() 调用我的一个 Django-Views 不起作用。 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. 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):根据文档:需要使用name关键字参数(kwarg):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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