gpt4 book ai didi

Django 通过 URL 传递 kwargs

转载 作者:行者123 更新时间:2023-12-05 08:34:34 27 4
gpt4 key购买 nike

这部分代码的目的是在类似于以下所示的模板中显示加入群组的所有请求:

Request 1 | Add | Delete
Request 2 | Add | Delete
Request 3 | Add | Delete
....

我想做的是使“添加”和“删除”按钮 href 成为 View 中的一个函数。但是我想知道将 **kwarg 从模板传递到 View 的正确方法是什么。否则是否有更好的方法来完成此操作?

模板

{% 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'),

查看group_judge_restart

def group_judge_request(request, gslug, decision):

查看group_requested_invites

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()!

最佳答案

我不认为有一种方法可以使用内置的 url 模板标签从模板中像这样传递 *kwargs。

有两种方法可以做到这一点,一种是创建两个单独的 url 并将决策作为 kwarg 传递给 View :

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 %}

关于Django 通过 URL 传递 kwargs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23166338/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com