提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、删除数据表信息

  1. 创建一个删除的html前端
  2. 在首页里添加跳转到删除界面的按钮
  3. urls.py里面添加相应的链接
  4. 在views.py中添加与删除相对应的方法

二、删除步骤

1. 创建一个删除的html前端

{% extends 'aoapp/index.html' %}
{% block title %}删除订单{% endblock %}
{% block content %}
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-4">
                <div style="height: 100px"></div>
                <div class="card shadow">
                    <div class="card-body text-center">
                        <div class="text_title">
                            <h4>你确定要删除这个订单吗?</h4>
                        </div>
                        <table class="table table-striped">
                            <thead class="thead-dark">
                            <tr>
                                <th scope="col">Customer</th>
                                <th scope="col">Product</th>
                                <th scope="col">Status</th>
                            </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>{{ order.customer }}</td>
                                    <td>{{ order.product }}</td>
                                    <td>{{ order.status }}</td>
                                </tr>
                            </tbody>
                        </table>
                        <form action="{% url 'deleteorder' order.id %}" method="post">
                            {% csrf_token %}
                            <a href="{% url 'homepage' %}" class="btn btn-warning">取消</a>
                            <button type="submit" class="btn btn-danger">确定</button>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col-md-4"></div>
        </div>
    </div>
{% endblock %}

2. 在首页里添加跳转到删除界面的按钮

 {% for i in order reversed %}
                        <tr>

                            <th scope="row">{{ i.id }}</th>
                            <td>{{ i.customer }}</td>
                            <td>{{ i.product }}</td>
                            <td>{{ i.time_created }}</td>
                            <td>{{ i.status }}</td>
                            <td>
                                <a href="{% url 'updateorder' i.id %}" class="btn btn-primary">update</a>
                            </td>
                            <td>
								<a href="{% url 'deleteorder' i.id %}" class="btn btn-dark text-light">remove</a>
							</td>
                        </tr>
                    {% endfor %}


3. urls.py里面添加相应的链接

因为在首页需要传递id进行查找并删除,str:pk这样,搭配{% url ‘deleteorder’ i.id %}

    path('deleteorder/<str:pk>/', delete_order,name='deleteorder'),

4. 在views.py中添加与删除相对应的方法

def delete_order(request, pk):
    order = Order.objects.get(id=pk)
    if request.method == "POST":
        order.delete()
        message = 'delete Successfully!'
        return redirect('homepage')

    context = {
        'order': order,
    }
    return render(request, "aoapp/deleteorder.html", context)

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐