enter code here
我有一个group_edit.html,它允许更新组的信息并删除它。
Upade(保存)效果很好,但是删除按钮无效。 感谢帮助: 我的group_edit.html:
{% block page %}
<form method="POST">
{% csrf_token %}
<div class="col-lg-4 col-md-4 col-sm-4 content">
{% bootstrap_form form %}
<button type="submit" class="btn btn-pink pull-right">Save</button>
<button type="reset" class="btn btn-warning pull-left"><a href="{% url 'confirm-delete' group_id %}"></a>Delete</button>
</div>
<a href="{% url 'group-list' %}">Back to list</a>
</form>
{% endblock %}
我的Confirm_delete.html模板:
{% block title %}Delete{% endblock %}
{% block heading %}<h3 class="page-header-center">Object Delete</h3> {% endblock %}
{% block page %}
<form method="post">{% csrf_token %}
<p>Are you sure you want to delete "{{ obj }}"?</p>
<input type="submit" value="Confirm" class="btn btn-warning">
<a href="group_list.html" class="btn btn-default">Cancel</a>
</form>
{% endblock %}
我的views.py:
def group_edit(request, group_id):
form = GroupForm(instance=Group.objects.get(group_id=group_id))
if request.method == "POST":
form = GroupForm(request.POST, instance=Group.objects.get(group_id=group_id))
if form.is_valid():
form.save()
messages.success(request, 'Group saved') # message for inform user of success - See messages in html file
return redirect(group_list)
return render(request, 'imports/group_edit.html', {
"group_id": group_id,
"form": form,
})
def confirm_delete(request, group_id):
obj = GroupForm(instance=Group.objects.get(group_id=group_id))
if request.method == "POST":
obj.delete()
messages.success(request, 'Deleted') # message for inform user of success - See messages in html file
return render(request, 'imports/group_list.html')
context = {
"obj": obj
}
return render(request, "imports/confirm_delete.html", context)
和我的urls.py:
path('group_edit/<int:group_id>/', views.group_edit, name='group-edit'),
path('confirm_delete/<int:group_id>/', views.confirm_delete, name='confirm-delete'),
答案 0 :(得分:1)
在您的链接中,<a>
的范围为空。所以代替
<a href="{% url 'confirm-delete' group_id %}"></a>Delete
应该是:
<a href="{% url 'confirm-delete' group_id %}">Delete</a>
最好用命名参数指定{% url ... %}
参数:
<a href="{% url 'confirm-delete' group_id=group_id %}">Delete</a>