如何从temlate将2个参数传递给django中的视图?

问题描述 投票:1回答:1

所以我知道我可以像这样传递一个参数:{%url'... path ...'argument%},但是我想传递2个像{%url'... path ...'argument1 arguments2 %}这是我的确切代码:search.html:

{% for id, title, thumbnail in videos%}
    <a href="https://www.youtube.com/watch?v={{id}}"><img src="{{thumbnail}}">{{title}}</a><p style="display:inline;">   |   </p><a href="{%url 'select' id title%}">Add to playlist</a>
    <br>
{%endfor%}

urls.py:

path('select/<id>/<title>', views.select, name='select'),

我收到以下错误:找不到带有参数'('omPiA5AsGWw','PRESLAVA-POSLEDEN ADRES /Преслава-Последенадрес,2007')的'select'反向。尝试了1个模式:['select /(?P [^ /] +)/(?P [^ /] +)$']

python django templates url view
1个回答
0
投票

您的标题包含斜线。您应将path指定为路径转换器

path('select/<id>/<path:title>', views.select, name='select'),

话虽这么说,但我不确定将此类标题编码为url是一个好主意,它将使丑陋的URL中包含许多百分比编码的字符。您可能需要查看slugs instead [Django-doc]

© www.soinside.com 2019 - 2024. All rights reserved.