1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| from django import forms from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User from django.core.exceptions import ValidationError from .models import Product, Review, Category
class ProductForm(forms.ModelForm): """产品表单""" class Meta: model = Product fields = ['title', 'slug', 'description', 'price', 'category', 'image', 'featured'] widgets = { 'title': forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': '请输入产品标题' }), 'slug': forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': '产品URL标识符' }), 'description': forms.Textarea(attrs={ 'class': 'form-control', 'rows': 5, 'placeholder': '请输入产品描述' }), 'price': forms.NumberInput(attrs={ 'class': 'form-control', 'step': '0.01', 'min': '0' }), 'category': forms.Select(attrs={ 'class': 'form-select' }), 'image': forms.FileInput(attrs={ 'class': 'form-control', 'accept': 'image/*' }), 'featured': forms.CheckboxInput(attrs={ 'class': 'form-check-input' }) } def clean_slug(self): """验证slug唯一性""" slug = self.cleaned_data['slug'] if Product.objects.filter(slug=slug).exclude(pk=self.instance.pk).exists(): raise ValidationError('该URL标识符已存在') return slug def clean_price(self): """验证价格""" price = self.cleaned_data['price'] if price <= 0: raise ValidationError('价格必须大于0') return price
class ReviewForm(forms.ModelForm): """评论表单""" class Meta: model = Review fields = ['rating', 'comment'] widgets = { 'rating': forms.Select( choices=[(i, f'{i}星') for i in range(1, 6)], attrs={'class': 'form-select'} ), 'comment': forms.Textarea(attrs={ 'class': 'form-control', 'rows': 4, 'placeholder': '请分享您的使用体验...' }) } def clean_comment(self): """验证评论内容""" comment = self.cleaned_data['comment'] if len(comment) < 10: raise ValidationError('评论内容至少需要10个字符') return comment
class ContactForm(forms.Form): """联系表单""" SUBJECT_CHOICES = [ ('general', '一般咨询'), ('support', '技术支持'), ('business', '商务合作'), ('complaint', '投诉建议'), ] name = forms.CharField( max_length=100, widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': '您的姓名' }) ) email = forms.EmailField( widget=forms.EmailInput(attrs={ 'class': 'form-control', 'placeholder': '您的邮箱' }) ) subject = forms.ChoiceField( choices=SUBJECT_CHOICES, widget=forms.Select(attrs={ 'class': 'form-select' }) ) message = forms.CharField( widget=forms.Textarea(attrs={ 'class': 'form-control', 'rows': 6, 'placeholder': '请详细描述您的问题或建议...' }) ) def clean_message(self): """验证消息内容""" message = self.cleaned_data['message'] if len(message) < 20: raise ValidationError('消息内容至少需要20个字符') return message
class CustomUserCreationForm(UserCreationForm): """自定义用户注册表单""" email = forms.EmailField( required=True, widget=forms.EmailInput(attrs={ 'class': 'form-control', 'placeholder': '邮箱地址' }) ) first_name = forms.CharField( max_length=30, required=True, widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': '名字' }) ) last_name = forms.CharField( max_length=30, required=True, widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': '姓氏' }) ) class Meta: model = User fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2') widgets = { 'username': forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': '用户名' }), } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['password1'].widget.attrs.update({ 'class': 'form-control', 'placeholder': '密码' }) self.fields['password2'].widget.attrs.update({ 'class': 'form-control', 'placeholder': '确认密码' }) def clean_email(self): """验证邮箱唯一性""" email = self.cleaned_data['email'] if User.objects.filter(email=email).exists(): raise ValidationError('该邮箱已被注册') return email def save(self, commit=True): """保存用户""" user = super().save(commit=False) user.email = self.cleaned_data['email'] user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] if commit: user.save() return user
|