Home > Blockchain >  Django: Problem with passing parameters from a template {% url %} to a view
Django: Problem with passing parameters from a template {% url %} to a view

Time:07-10

I have this urlpath:

path('download/<str:fpath>/<str:fname>', views.download, name='download'),

And this is the view:

def download(request, fpath, fname):
    # some code

In template, I have this href tag and I want to pass those strings as arguments to the download view.

<a href="{% url 'download' 'lms/static/lms/files/homework/Math 1/3/3' 'hmm.pdf' %}">click me</a>

But I get this error:

NoReverseMatch at /

Reverse for 'download' with arguments '('lms/static/lms/files/homework/Math 1/3/3', 'hmm.pdf')' not found. 1 pattern(s) tried: ['download/(?P<fpath>[^/] )/(?P<fname>[^/] )\\Z']

How can I fix this?

CodePudding user response:

Url arguments of type str cannot contains / characters. You can see this in the error message which has translated your <str:fpath> to a regex:

tried: ['download/(?P<fpath>[^/] )/(?P<fname>[^/] )\\Z']

You should use a path converter for your case (see here).

For example:

path('download/<path:fpath>/<str:fname>', ...)
  • Related