Python 未找到参数“(u'd3…,)”和关键字参数“{}”的反转。尝试了1个模式
我想在我的django模板主页中使用django反向url.html: ] 在my views.py中,我有主页和文件下载视图的代码: 但当我在浏览器中测试download url download/something_时,我出现了以下错误: 对于参数为“u”d3b63b8895e15a8722668bd1da752b47842d13fa“和关键字参数“{}”的“fileDownloadPage”,则相反。尝试了1种模式:[“下载/?P[\w]+/$”] home-page.html页面中的以下语句有什么问题:Python 未找到参数“(u'd3…,)”和关键字参数“{}”的反转。尝试了1个模式,python,django,django-templates,Python,Django,Django Templates,我想在我的django模板主页中使用django反向url.html: ] 在my views.py中,我有主页和文件下载视图的代码: 但当我在浏览器中测试download url download/something_时,我出现了以下错误: 对于参数为“u”d3b63b8895e15a8722668bd1da752b47842d13fa“和关键字参数“{}”的“fileDownloadPage”,则相反。尝试了1种模式:[“下载/?P[\w]+/$”] home-page.html页面中的以下
Django需要一个尾部斜杠。所以这个URL:
url(r'^download/(?P<hash>[\w]+/$)', views.file_download, name="fileDownloadPage"),
致:
在当前正则表达式r'^download/?p[\w]+/$中,完整的捕获组由r'?p[\w]+/$'定义。捕获组包括尾部斜杠,当反转时,完整的捕获组将替换为您的参数。当您传递一个不包含尾部斜杠的参数时,它将在中被替换,但它与原始正则表达式不匹配。这就是Django提出NoReverseMatch的原因
这就是为什么您需要将尾部斜杠从捕获组中移出的原因,正如Aron所建议的:
url(r'^download/(?P<hash>\w+)/$', views.file_download, name="fileDownloadPage"),
你试过{%url'fileDownloadPage'hash=entry.2%}吗?为什么你有{%url'fileDownloadPage'entry.2%}{{entry.2}?我想应该是href attr中的{%url'fileDownloadPage'条目.2%}。@RetoAebersold谢谢。这就是工作。但奇怪的是,当我撤销你的更改时,一切都会好起来,我再也看不到那个错误了!!!!我不知道为什么:@Gocht是的。你是对的。这是我的错误。仅供参考,“zippedContent”:。。。写得更好[f.file.name,f.privacy,f,当前组中f的哈希值。file\u set.all]
@login_required
def home_page(request):
current_group = Group.objects.get(name=request.user.username)
return render(request, 'home-page.html', {
'zippedContent': zip([f.file.name for f in current_group.file_set.all()],
[f.privacy for f in current_group.file_set.all()],
[f.hash for f in current_group.file_set.all()])
})
@login_required
def file_download(request, requested_file_hash):
try:
requested_file = File.objects.get(hash=requested_file_hash)
response = HttpResponse(mimetype='application/force-download')
response['Content-Disposition'] = 'attachment; filename=%s'\
% smart_str(requested_file.file.name)
response['X-Sendfile'] = smart_str(requested_file.file.url)
return response
except ObjectDoesNotExist:
return Http404()
<a href="{% url 'fileDownloadPage' entry.2 %}{{ entry.2 }}">
url(r'^download/(?P<hash>[\w]+/$)', views.file_download, name="fileDownloadPage"),
url(r'^download/(?P<hash>\w+)/$', views.file_download, name="fileDownloadPage"),
<a href="{% url 'fileDownloadPage' entry.2 %}{{ entry.2 }}">
<a href="{% url 'fileDownloadPage' entry.2 %}">{{ entry.2 }}</a>
url(r'^download/(?P<hash>\w+)/$', views.file_download, name="fileDownloadPage"),