-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
76 lines (58 loc) · 2.48 KB
/
forms.py
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
from django import forms
from django.forms import CharField
from django.forms import widgets
from .models import Account
from store.models import Product
from category.models import Category
from django.views.generic.edit import FormView
class MultipleFileInput(forms.ClearableFileInput):
allow_multiple_selected = True
class MultipleFileField(forms.FileField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("widget", MultipleFileInput())
super().__init__(*args, **kwargs)
def clean(self, data, initial=None):
single_file_clean = super().clean
if isinstance(data, (list, tuple)):
result = [single_file_clean(d, initial) for d in data]
else:
result = single_file_clean(data, initial)
return result
class MultipleImageUploadForm(forms.Form):
file_field = MultipleFileField()
class UploadProductsForm(forms.ModelForm):
class Meta:
model = Product
fields = ['product_name', 'price', 'stock', 'category', 'description']
class ProfileChangeForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField(max_length=100)
phone_number = forms.CharField(max_length=50)
class RegistrationForm(forms.ModelForm):
name = forms.CharField(max_length=100)
phone_number = forms.CharField(max_length=50)
email = forms.EmailField(max_length=50)
password = forms.CharField(widget=forms.PasswordInput(attrs={
'placeholder': 'Mật khẩu'
}))
confirm_password = forms.CharField(widget=forms.PasswordInput(attrs={
'placeholder': 'Nhập lại mật khẩu'
}))
class Meta:
model = Account
fields = ['name', 'phone_number', 'email', 'password']
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
self.fields['name'].widget.attrs['placeholder'] = 'Họ và tên'
self.fields['phone_number'].widget.attrs['placeholder'] = 'Số điện thoại'
self.fields['email'].widget.attrs['placeholder'] = 'Email'
for field in self.fields:
self.fields[field].widget.attrs['class'] = 'form-control'
def clean(self):
cleaned_data = super(RegistrationForm, self).clean()
password = cleaned_data.get('password')
confirm_password = cleaned_data.get('confirm_password')
if password != confirm_password:
raise forms.ValidationError(
'Mật khẩu không trùng khớp!'
)