链接到具有表单属性的另一模板

时间:2019-06-09 21:51:35

标签: django templates

我有一个带有删除按钮的表格。我具有所有代码删除功能和确认。我的问题是在删除按钮中添加href引用。我使用ModelForm,但我认为链接无法正常工作:错误消息:找不到带有参数'(',)'的'confirm-delete'相反。尝试了1种模式:['import \ / confirm_delete \ /(?P [0-9] +)\ / $']

尝试了几种方法来定义良好的url,但仍然无法正常工作。我认为问题在于识别pk的论点并不容易

这是我的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' form.group_id %}"></a>Delete</button>
        </div>
        <a href="{% url 'group-list' %}">Back to list</a>
    </form>
{% endblock %}

这是我的views.py(删除):

def confirm_delete(request, group_id):
    obj = get_object_or_404(Group, 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('confirm_delete/<int:group_id>/', views.confirm_delete, name='confirm-delete'),

我的删除确认页面:

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

点击后进入我的确认删除页面

0 个答案:

没有答案