此部分代码的目的是显示在模板中加入组的所有请求,类似于以下所示:
Request 1 | Add | Delete
Request 2 | Add | Delete
Request 3 | Add | Delete
....
我想要做的是将“添加”和“删除”按钮href设置为视图中的一个功能。但是我想知道将** kwarg从模板传递到视图的正确方法是什么。否则,如果有更好的方法来实现这个目标吗?
{% for asking in requested %}
<a href={% url 'group_judge_request' group_profile.slug decision=0 %}>Cut {{ asking.user.username }}</a>
<a href={% url 'group_judge_request' group_profile.slug decision=1 %}>Keep {{ asking.user.username }}</a>
{% endfor %}
url(r'^judge_request/(?P<gslug>[\w-]+)$',
group_judge_request,
kwargs={'decision':'decision'},
name='group_judge_request'),
def group_judge_request(request, gslug, decision):
def group_requested_invites(request, gslug):
....
requested = GroupRequestToJoin.objects.filter(group=group_profile.group, checked=False)
return render(request, "groups/group_requested_invites.html", {
'requested' : requested,
})
错误:
Don't mix *args and **kwargs in call to reverse()!
答案 0 :(得分:4)
我认为你想使用网址查询。所以你的网址如下:
<a href="{% url 'group_judge_request' group_profile.slug %}?decision=0">Cut {{asking.user.username }}</a>
然后,您可以使用以下命令继续列出查询:
request.META['QUERY_STRING']
答案 1 :(得分:4)
我认为没有办法使用内置的url模板标记从模板中传递这样的* kwargs。
有两种方法可以做到这一点,一种是创建两个独立的网址,并将决定作为kwarg传递给视图:
<强> urls.py 强>
url(r'^judge_request_cut/(?P<gslug>[\w-]+)$',
group_judge_request,
kwargs={'decision': 0},
name='group_judge_request_cut'),
url(r'^judge_request_keep/(?P<gslug>[\w-]+)$',
group_judge_request,
kwargs={'decision': 1},
name='group_judge_request_keep'),
<强>模板强>
{% for asking in requested %}
<a href={% url 'group_judge_request_cut' group_profile.slug decision=0 %}>Cut {{ asking.user.username }}</a>
<a href={% url 'group_judge_request_keep' group_profile.slug decision=1 %}>Keep {{ asking.user.username }}</a>
{% endfor %}
或者您可以将整数作为参数传递:
<强> urls.py 强>
url(r'^judge_request/(?P<gslug>[\w-]+)/(?P<decision>\d{1})$',
group_judge_request,
name='group_judge_request'),
<强>模板强>
{% for asking in requested %}
<a href={% url 'group_judge_request' group_profile.slug 0 %}>Cut {{ asking.user.username }}</a>
<a href={% url 'group_judge_request' group_profile.slug 1 %}>Keep {{ asking.user.username }}</a>
{% endfor %}