将模板变量添加到URL中并满足反向匹配

发布于 2025-01-31 04:59:37 字数 958 浏览 1 评论 0 原文

我找到了一种将变量注入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']

I found a way to inject variables into the URL per Django Template: Add Variable Into URL As Parameter, but my reverse match is not working. Do I need to use re-path and use regular expressions?

Current url.py

    path('student/item/drilldown/<str:type>/<int:num>/',views.StudentBehaviorListView.as_view(),name='student_item_list_drilldown'),

Original w/hard-coding:

<a href="{%url 'registration:student_item_list_drilldown' type='grade' num=6%}">

With variable injection:

<a href="{%url 'registration:student_item_list_drilldown'%}?type=grade&num={{i.grade}}">{{i.grade}}th Grade</a>

Error message:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪#恶を代价 2025-02-07 04:59:37

这样:

{% url 'registration:student_item_list_drilldown' %}

将尝试在将其读取为HTML之前,尝试找到名为 student_item_list_drilldown 的路径。这就是为什么它尝试(和失败)找到&lt; str:type&gt; and &lt; int:num&gt; in {%url'regustration:sut力地_item_list_drilllilldown'% } 。您必须在 {%url ...%} 括号内包含该变量。

最好的方法是:

{% url 'registration:student_item_list_drilldown' 'grade' i.grade %}

This:

{% url 'registration:student_item_list_drilldown' %}

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:

{% url 'registration:student_item_list_drilldown' 'grade' i.grade %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文