簡體   English   中英

在主應用程序中反轉 url 時找不到 Django Reverse

[英]Django Reverse Not Found when reversing a url in main app

我不明白為什么這個簡單的 reverse() 調用我的一個 Django-Views 不起作用。 在我的一些測試中出現錯誤:

反向代碼片段:

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

網址注冊:

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

看法:

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

我不斷收到的錯誤是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.

任何幫助表示贊賞。

根據文檔:需要使用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