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

Django中勾选复选框时另一个页面显示未勾选问题排查

问题根源分析
  1. 布尔字段与字符串比较不匹配:如果Progress模型中的ISBNsent和ISBNdelivered是BooleanField类型,模板中book_progress.ISBNsent == 'True'的判断逻辑错误——数据库存储的是布尔值True/False,和字符串'True'比较会返回False,导致本该显示图标的区域错误显示复选框。
  2. POST处理后未重定向:当前视图处理POST请求后未返回重定向,直接渲染页面。虽修改后的实例已保存到数据库,但浏览器可能仍使用旧的页面上下文,且存在刷新重复提交的风险,无法确保页面加载最新数据。
修复步骤

1. 修正视图函数的字段赋值与重定向逻辑

将POST获取的字符串值转为布尔类型,并在处理完成后重定向到当前页面:

from django.shortcuts import redirect, reverse, get_object_or_404
from datetime import datetime
from .models import Book, Progress

def bookSteps(request, book_id):
    book_page = get_object_or_404(Book, pk=book_id)
    book_progress = Progress.objects.get(BookID=book_id)
    
    if request.method == 'POST': 
        if 'ISBNsent' in request.POST:
            # 转换字符串为布尔值
            book_progress.ISBNsent = request.POST.get("ISBNsent") == 'True'
            book_progress.ISBN_sentDate = datetime.now().date() 
            book_progress.save()
        elif 'ISBNdelivered' in request.POST:
            book_progress.ISBNdelivered = request.POST.get("ISBNdelivered") == 'True'
            book_progress.ISBN_deliveredDate = datetime.now().date() 
            book_progress.save()
        # 重定向到当前页面,确保加载最新数据
        return redirect(reverse('tracker:bookSteps', args=[book_id]))
    
    # GET请求渲染页面
    return render(request, '你的模板文件名.html', {'book_page': book_page, 'book_progress': book_progress})

2. 修正模板的判断逻辑

将字符串比较改为直接判断布尔值:

<!-- ISBNsent 表单 -->
<form id='ISBN_form' action="{%url 'tracker:bookSteps' book_page.id%}" method="POST">
    {% csrf_token %}
    <div class="columns is-mobile">
        {% if book_progress.ISBNsent %}
        <div class="content column is-5">
            <label class="checkbox" dir="rtl">
                <i class="fa-solid fa-check has-text-success"></i>
                تم التسليم
            </label>
        </div>
        {%else%}
        <div class="content column is-5">
            <label class="checkbox" dir="rtl">
                <input type="checkbox" name="ISBNsent" id="ISBNsent" value="True" onchange="submitCheckbox();">
                    تم التسليم
            </label>
        </div>
        {%endif%}
    </div>
</form>

<!-- ISBNdelivered 表单(同理修改) -->
<form id='ISBN_delivered_form' action="{%url 'tracker:bookSteps' book_page.id%}" method="POST">
    {% csrf_token %}
    <div class="columns is-mobile">
        {% if book_progress.ISBNdelivered %}
        <div class="content column is-5">
            <label class="checkbox" dir="rtl">
                <i class="fa-solid fa-check has-text-success"></i>
                <!-- 对应阿拉伯文本 -->
            </label>
        </div>
        {%else%}
        <div class="content column is-5">
            <label class="checkbox" dir="rtl">
                <input type="checkbox" name="ISBNdelivered" id="ISBNdelivered" value="True" onchange="submitCheckbox();">
                    <!-- 对应阿拉伯文本 -->
            </label>
        </div>
        {%endif%}
    </div>
</form>

3. 确认模型字段类型

确保Progress模型使用布尔字段存储状态:

from django.db import models

class Progress(models.Model):
    BookID = models.ForeignKey(Book, on_delete=models.CASCADE)
    ISBNsent = models.BooleanField(default=False)
    ISBN_sentDate = models.DateField(null=True, blank=True)
    ISBNdelivered = models.BooleanField(default=False)
    ISBN_deliveredDate = models.DateField(null=True, blank=True)
    # 其他字段...
验证修复
  1. 勾选任意复选框,表单提交后页面会重定向并重新加载。
  2. 检查数据库中两个字段的布尔值是否正确保存。
  3. 页面将根据数据库中的布尔值,正确显示对应图标或复选框。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 23:07:54