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

31 lines
1.3 KiB

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.

# zy:comments/forms.py
#zy:评论(Comment)模型的表单定义文件
#zy: 该文件定义了用于创建和编辑评论的表单类
#zy: 导入Django表单模块提供表单相关的基类和功能
from django import forms
#zy: 从当前包(models模块)导入Comment模型
from .models import Comment
#zy: 定义评论表单类继承自ModelForm自动根据模型字段生成表单
class CommentForm(forms.ModelForm):
# zy:定义表单的元数据类,用于配置表单与模型的关联和行为
class Meta:
# zy:指定表单关联的模型为Comment
model = Comment
#zy: 指定表单中包含的字段,这里只包含评论内容字段
fields = ['content']
#zy: 配置字段的小部件(Widget)属性控制表单字段的HTML渲染
widgets = {
# 为content字段配置Textarea文本域小部件
'content': forms.Textarea(attrs={
'rows': 4, # 设置文本域行数为4行
'placeholder': '请输入您的评论...', # 设置占位符文本
'class': 'comment-textarea' # 设置CSS类名用于样式控制
})
}
# zy:配置字段的标签显示
labels = {
'content': '' # zy:将content字段的标签设为空不显示标签文本
}