You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DjangoBlog/comments/forms.py

21 lines
1021 B

This file contains ambiguous Unicode characters!

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'