wasp256
wasp256

Reputation: 6242

django href url with parameter throwing error

I have the following setup for a simple href download page:

urls.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'),   
]

template.html:

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

views.py:

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

But when executing I get the following error:

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 output of the related files:

$ 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

Upvotes: 0

Views: 86

Answers (2)

Ernest
Ernest

Reputation: 2949

From what you've provided it seems like the urls.py you are showing belongs to one of the applications within the project. My guess is that URLs of that application are either not included properly or included with a namespace.

Upvotes: 1

Waket Zheng
Waket Zheng

Reputation: 6331

why not use django2.0+? then code may as below:

urls.py

path('fileformaterror/download/<s3_key>/', FileFormatErrorDownloadView.as_view(), name='file-format-error-download')

template.html

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

views.py

from django.shortcuts import HttpResponse
class FileFormatErrorDownloadView(View):
    def get(self, request, s3_key):
        return HttpResponse('success')

Upvotes: 0

Related Questions