在Django中:
re_path(r'^accept_friend_request(?P<uidb64>[0-9A-Za-z_\-]+)/$', accept_friend_request, name = 'accept_friend_request')
如何在模板的url
和re_path
的字符串模式中将状态作为参数添加?
<a class="btn btn-primary" href="{% url 'accept_friend_request' uidb64=uid %}">Accept</a>
这是我使用的代码:
def accept_friend_request(request, uidb64, status):
"""Accept button will lead to entry in database as accepted and reject button will lead to entry in database as rejected based on status flag"""
uid = urlsafe_base64_decode(uidb64).decode()
friend_user = User.objects.get(pk=Friend.to_user.id)
f = Friend.objects.filter(friend_id=friend_user)
if f:
f.status = status
f.save()
return request, "users/friend_list.html", {"uid": uidb64, "status": status}`
else:
return render(request, 'blog/base.html')
先谢谢您
答案 0 :(得分:0)
# your url should include status
re_path(r'^accept_friend_request(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<status>[\w-]+)/$', accept_friend_request, name = 'accept_friend_request')