You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django 5.1 CRUD删除功能报错:视图未返回HttpResponse对象

Django 5.1 CRUD删除功能报错:视图未返回HttpResponse对象

问题详情

用Django 5.1开发CRUD功能,点击删除按钮后页面无变化,控制台抛出错误:

The view stock.views.eliminar_tipomaterial didn't return an HttpResponse object. It returned None instead

相关代码

views.py

def eliminar_tipomaterial(request, tipomaterial_id):
    tipomaterial = get_object_or_404(TipoMaterial, pk=tipomaterial_id)
    if request.method == 'POST':
        tipomaterial.delete()
        return redirect('tipomaterial.html')

Tipomaterial.html

<main class="container">
    <div class="table-responsive">
        <table class="table table-bordered">
            <thead>
                <tr style="text-align: center;">
                    <th scope="col">Nombre del material</th>
                    <th scope="col" colspan="2">Acciones</th>
                </tr>
            </thead>
            <tbody class="table-group-divider">
                {% for datos in tipomaterial %}
                <tr>    
                    <td>
                        <li class="list-group-item" href="{% url 'detalle_tipomaterial' datos.id %}">               
                            <strong>{{datos.nombre}}</strong>              
                        </li>
                    </td>
                    <td>                   
                        <a class="btn btn-primary" href="{% url 'detalle_tipomaterial' datos.id %}" role="button">        
                            <i class="bi bi-pencil-fill">
                                Editar
                            </i>       
                        </a>  
                        <a class="btn btn-danger" href="{% url 'eliminar_tipomaterial' datos.id %}" role="button">
                            <i class="bi bi-trash3-fill">
                                Eliminar
                            </i>              
                        </a>       
                    </td>
                </tr>
                {% endfor%}
            </tbody>    
        </table>
    </div>
</main>

问题原因

  1. 请求方式不匹配:视图仅处理POST请求,但删除按钮用的是<a>标签,默认发送GET请求,此时视图没有返回任何HttpResponse对象,直接返回None触发报错。
  2. 重定向目标错误:redirect('tipomaterial.html')写法错误,redirect应传入urls.py中定义的URL名称,而非模板文件名。

解决方法(遵循Django最佳实践)

第一步:将删除按钮改为POST表单

GET请求不应用于修改/删除数据,把模板中的删除<a>标签替换为表单:

<td>                   
    <a class="btn btn-primary" href="{% url 'detalle_tipomaterial' datos.id %}" role="button">        
        <i class="bi bi-pencil-fill">
            Editar
        </i>       
    </a>  
    <!-- 替换原a标签为表单 -->
    <form action="{% url 'eliminar_tipomaterial' datos.id %}" method="POST" style="display: inline;">
        {% csrf_token %}
        <button type="submit" class="btn btn-danger" onclick="return confirm('确定要删除该物料类型吗?')">
            <i class="bi bi-trash3-fill">
                Eliminar
            </i>              
        </button>
    </form>       
</td>
  • {% csrf_token %}是Django强制要求的跨站请求防护,必须添加。
  • 加入confirm弹窗可避免误操作删除。

第二步:修正视图的重定向与GET请求处理

先确认urls.py中列表页面的URL名称,示例配置如下:

# urls.py
from django.urls import path
from . import views

urlpatterns = [
    # 列表页面URL,name设为'list_tipomaterial'
    path('tipomaterial/', views.tipomaterial_list, name='list_tipomaterial'),
    path('tipomaterial/eliminar/<int:tipomaterial_id>/', views.eliminar_tipomaterial, name='eliminar_tipomaterial'),
    # 其他URL...
]

修改视图:

def eliminar_tipomaterial(request, tipomaterial_id):
    tipomaterial = get_object_or_404(TipoMaterial, pk=tipomaterial_id)
    if request.method == 'POST':
        tipomaterial.delete()
        # 用URL名称重定向到列表页,替换为你实际的URL名称
        return redirect('list_tipomaterial')
    # 若有人用GET访问删除接口,直接跳回列表页避免报错
    return redirect('list_tipomaterial')

修改完成后,点击删除按钮会弹出确认框,提交POST请求完成删除后自动跳回列表页,不会再出现视图返回None的错误。

内容的提问来源于stack exchange,提问作者Andres Vilca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 00:50:09