首页 > 解决方案 > 带有参数抛出错误的 django href url

问题描述

我对一个简单的href下载页面进行了以下设置:

网址.py

urlpatterns = [
    url(r'^kpis/$', InternalKPIView.as_view(), name='internal_kpis'),
    url(r'^tenants/$', TenantListView.as_view(), name='tenant-list'),
    url(r'^tenants/(?P<pk>[0-9]+)/$', TenantStatsView.as_view(), name='tenant-stats'),
    url(r'^fileformaterror/$', FileFormatErrorView.as_view(), name='file-format-error'),
    url(r'^fileformaterror/download/(?P<s3_key>.*)$', FileFormatErrorDownloadView.as_view(), name='file-format-error-download'),   
]

模板.html:

<a href="{% url 'file-format-error-download' s3_key=file.s3_key %}" target="_blank">Download</a>

视图.py:

class FileFormatErrorDownloadView(View):
    def get(self, request, s3_key):
        pass

但是执行时出现以下错误:

django.urls.exceptions.NoReverseMatch: Reverse for 'file-format-error-download' not found. 'file-format-error-download' is not a valid view function or pattern name.

相关文件的树输出:

$ tree -I "*.pyc|__pycache__"
.
├── apps.py
├── __init__.py
├── migrations
│   └── __init__.py
├── templates
│   └── backoffice
│    ├── file_format_error.html
│    └── internal_kpis.html
├── urls.py
└── views.py

3 directories, 7 files

标签: pythondjango

解决方案


从您提供的内容来看,urls.py您所展示的似乎属于项目中的一个应用程序。我的猜测是该应用程序的 URL 要么未正确包含,要么包含在命名空间中。


推荐阅读