将模板变量添加到URL中并满足反向匹配
我找到了一种将变量注入url per 进入URL,但我的反向匹配不起作用。我需要使用重复路径并使用正则表达式吗?
当前url.py
path('student/item/drilldown/<str:type>/<int:num>/',views.StudentBehaviorListView.as_view(),name='student_item_list_drilldown'),
原始编码:
<a href="{%url 'registration:student_item_list_drilldown' type='grade' num=6%}">
带有变量注入:
<a href="{%url 'registration:student_item_list_drilldown'%}?type=grade&num={{i.grade}}">{{i.grade}}th Grade</a>
错误消息:
NoReverseMatch at /registration/dashboard/
Reverse for 'student_item_list_drilldown' with no arguments not found. 1 pattern(s) tried: ['registration/student/item/drilldown/(?P<type>[^/]+)/(?P<num>[0-9]+)/\\Z']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样:
将尝试在将其读取为HTML之前,尝试找到名为
student_item_list_drilldown
的路径。这就是为什么它尝试(和失败)找到&lt; str:type&gt;
and&lt; int:num&gt;
in{%url'regustration:sut力地_item_list_drilllilldown'% }
。您必须在{%url ...%}
括号内包含该变量。最好的方法是:
This:
Will try to find path named
student_item_list_drilldown
before it is read as html. That's why it tries (and fails) to find<str:type>
and<int:num>
in{% url 'registration:student_item_list_drilldown' %}
. You have to include that variables inside{% url ... %}
brackets.The best approach would be that: