|
|
from django import forms
|
|
|
from .models import Post, Category, PrimaryTag, SecondaryTag
|
|
|
from comments.models import Comment # 从comments应用导入
|
|
|
|
|
|
class CommentForm(forms.ModelForm):
|
|
|
class Meta:
|
|
|
model = Comment
|
|
|
fields = ['content']
|
|
|
widgets = {
|
|
|
'content': forms.Textarea(attrs={
|
|
|
'rows': 4,
|
|
|
'placeholder': '请输入您的评论...',
|
|
|
'class': 'comment-textarea'
|
|
|
})
|
|
|
}
|
|
|
labels = {
|
|
|
'content': ''
|
|
|
}
|
|
|
|
|
|
class PostForm(forms.ModelForm):
|
|
|
class Meta:
|
|
|
model = Post
|
|
|
fields = ['title', 'content', 'excerpt', 'category', 'primary_tags', 'secondary_tags', 'featured_image']
|
|
|
widgets = {
|
|
|
'title': forms.TextInput(attrs={
|
|
|
'class': 'form-control',
|
|
|
'placeholder': '请输入文章标题'
|
|
|
}),
|
|
|
'content': forms.Textarea(attrs={
|
|
|
'class': 'form-control',
|
|
|
'rows': 15,
|
|
|
'placeholder': '请输入文章内容...'
|
|
|
}),
|
|
|
'excerpt': forms.Textarea(attrs={
|
|
|
'class': 'form-control',
|
|
|
'rows': 3,
|
|
|
'placeholder': '请输入文章摘要(可选)'
|
|
|
}),
|
|
|
'category': forms.Select(attrs={'class': 'form-control'}),
|
|
|
'primary_tags': forms.SelectMultiple(attrs={'class': 'form-control'}),
|
|
|
'secondary_tags': forms.SelectMultiple(attrs={'class': 'form-control'}),
|
|
|
'featured_image': forms.FileInput(attrs={'class': 'form-control'})
|
|
|
}
|
|
|
labels = {
|
|
|
'title': '文章标题',
|
|
|
'content': '文章内容',
|
|
|
'excerpt': '文章摘要',
|
|
|
'category': '分类',
|
|
|
'primary_tags': '一级标签',
|
|
|
'secondary_tags': '二级标签',
|
|
|
'featured_image': '特色图片'
|
|
|
}
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
# HJH:调用父类的初始化方法
|
|
|
super().__init__(*args, **kwargs)
|
|
|
# HJH:设置分类字段的查询集,限制为所有已存在的分类
|
|
|
self.fields['category'].queryset = Category.objects.all()
|
|
|
# HJH:设置一级标签字段的查询集,限制为所有已存在的一级标签
|
|
|
self.fields['primary_tags'].queryset = PrimaryTag.objects.all()
|
|
|
# HJH:设置二级标签字段的查询集,限制为所有已存在的二级标签
|
|
|
self.fields['secondary_tags'].queryset = SecondaryTag.objects.all() |