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

Django函数视图POST请求后重渲染页面更新表格内容问题

Django 函数视图实现表单筛选后重新渲染表格

问题分析

原代码存在几个关键问题导致无法正常渲染更新后的内容:

  • 请求方法判断错误:使用赋值运算符=而非比较运算符==,且POST未加引号
  • 未从POST请求中获取筛选参数:process变量未定义,无法正确筛选BaseModel
  • 缺少对筛选参数的默认处理:当POST无参数时可能出现异常

修正后的视图代码

from django.shortcuts import render
from .models import OrderModel, BaseModel

def end_to_end_tracker_view(request, *args, **kwargs):
    def get_context(order_obj, base_obj):
        # 生成上下文,包含数据和筛选回显参数
        return {
            'orders': order_obj,
            'bases': base_obj,
            'selected_process': request.POST.get('process', '')
        }

    if request.method == "POST":
        # 从POST请求中获取筛选参数
        process = request.POST.get('process', None)
        
        # 筛选订单数据,可根据需求调整规则
        order_obj = OrderModel.objects.filter(status=True)
        
        # 根据筛选参数处理BaseModel查询
        if process:
            base_obj = BaseModel.objects.filter(process=process)
        else:
            base_obj = BaseModel.objects.all()
            
        context = get_context(order_obj, base_obj)
        return render(request, "app/end_to_end_tracker.html", context)
    
    # GET请求时返回全量数据
    order_obj = OrderModel.objects.all()
    base_obj = BaseModel.objects.all()
    context = get_context(order_obj, base_obj)
    return render(request, "app/end_to_end_tracker.html", context)

模板代码(end_to_end_tracker.html)

需要包含提交到当前视图的表单,以及渲染数据的表格:

<!-- 筛选表单 -->
<form method="POST" action="">
    {% csrf_token %}
    <label for="process">选择流程:</label>
    <select name="process" id="process">
        <option value="" {% if selected_process == "" %}selected{% endif %}>全部</option>
        <!-- 可根据实际业务动态生成选项,或硬编码可选值 -->
        <option value="process1" {% if selected_process == "process1" %}selected{% endif %}>流程1</option>
        <option value="process2" {% if selected_process == "process2" %}selected{% endif %}>流程2</option>
    </select>
    <button type="submit">筛选</button>
</form>

<!-- 订单数据表格 -->
<table border="1">
    <thead>
        <tr>
            <th>订单ID</th>
            <th>状态</th>
        </tr>
    </thead>
    <tbody>
        {% for order in orders %}
        <tr>
            <td>{{ order.id }}</td>
            <td>{{ order.status|yesno:"已完成,未完成" }}</td>
        </tr>
        {% empty %}
        <tr>
            <td colspan="2">暂无匹配的订单数据</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

<!-- BaseModel数据表格 -->
<table border="1">
    <thead>
        <tr>
            <th>流程名称</th>
            <!-- 其他表头字段 -->
        </tr>
    </thead>
    <tbody>
        {% for base in bases %}
        <tr>
            <td>{{ base.process }}</td>
            <!-- 其他表格内容 -->
        </tr>
        {% empty %}
        <tr>
            <td colspan="1">暂无匹配的流程数据</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

关键注意点

  • 表单必须包含{% csrf_token %},否则Django会拦截POST请求
  • 通过selected_process回显用户选择的筛选值,提升交互体验
  • 可根据业务需求扩展筛选条件,比如添加日期范围、多下拉框等

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 20:10:16