This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# 导入Django表单相关模块
from django import forms
from django.forms import ModelForm # 导入模型表单类,用于快速生成与模型对应的表单
# 导入当前应用下的Comment模型,表单将基于该模型创建
from .models import Comment
# 定义评论表单类,继承自ModelForm(模型表单)
class CommentForm(ModelForm):
# 定义父评论ID字段,用于实现评论回复功能
# IntegerField:整数类型字段,存储父评论的ID
# widget=forms.HiddenInput:使用隐藏输入框,不在页面上显示但会随表单提交
# required=False:该字段为非必填,顶级评论(无父评论)不需要填写
parent_comment_id = forms.IntegerField(
widget=forms.HiddenInput, required=False)
# Meta类用于配置模型表单的元数据
class Meta:
model = Comment # 指定表单对应的模型为Comment
fields = ['body'] # 指定需要在表单中显示的模型字段,这里只包含评论内容字段'body'