我无法理解为什么对我的Django-Views进行这种简单的reverse()调用无法正常工作的原因。我的某些测试中出现错误:
反向代码段:
# myapp/tests.py
response = self.client.get(reverse('index', args=()))
URL注册表:
# 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.
感谢任何帮助。
按照docs:需要使用name关键字参数(kwarg):
# myapp/urls.py
urlpatterns = [
path('', views.index, name='index'),
path('configuration/', include('configuration.urls')),
path('admin/', admin.site.urls),
]