简体   繁体   English

Django:将参数从模板 {% url %} 传递到视图时出现问题

[英]Django: Problem with passing parameters from a template {% url %} to a view

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.在模板中,我有这个 href 标签,我想将这些字符串作为参数传递给download视图。

<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?我怎样才能解决这个问题?

Url arguments of type str cannot contains / characters. str类型的 URL 参数不能包含/字符。 You can see this in the error message which has translated your <str:fpath> to a regex:您可以在将您的<str:fpath>转换为正则表达式的错误消息中看到这一点:

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

You should use a path converter for your case (see here ).您应该为您的案例使用path转换器(请参见此处)。

For example:例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM