SHOW:
|
|
- or go back to the newest paste.
1 | # Django Basics Exam Helper | |
2 | ||
3 | ||
4 | -------------------------------------------------------------------------------------- | |
5 | # How-to-add-class-and-other-atrributes-to-form-fields-in-django -> | |
6 | # https://www.letscodemore.com/blog/how-to-add-class-and-other-atrributes-to-form-fields-in-django/ | |
7 | -------------------------------------------------------------------------------------- | |
8 | # Django form field types: Widgets, options and validations -> | |
9 | # https://www.webforefront.com/django/formfieldtypesandvalidation.html | |
10 | -------------------------------------------------------------------------------------- | |
11 | # How to setup Django project from 0 -> | |
12 | # https://pastebin.com/V5a9GanA | |
13 | -------------------------------------------------------------------------------------- | |
14 | ||
15 | ||
16 | -------------------------------------------------------------------------------------- | |
17 | # Disable fields and delete: | |
18 | ||
19 | class AlbumDeleteForm(AlbumBaseForm): | |
20 | def __init__(self, *args, **kwargs): | |
21 | super().__init__(*args, **kwargs) | |
22 | self.__disable_fields() | |
23 | ||
24 | def save(self, commit=True): | |
25 | if commit: | |
26 | self.instance.delete() | |
27 | return self.instance | |
28 | ||
29 | def __disable_fields(self): | |
30 | for field in self.fields.values(): | |
31 | field.widget.attrs['disabled'] = True | |
32 | field.widget.attrs['readonly'] = True | |
33 | field.required = False | |
34 | ||
35 | ||
36 | -------------------------------------------------------------------------------------- | |
37 | # BaseForm ,labels and placeholders: | |
38 | ||
39 | class AlbumBaseForm(forms.ModelForm): | |
40 | class Meta: | |
41 | model = AlbumModel | |
42 | fields = '__all__' | |
43 | ||
44 | labels = { | |
45 | 'image_url': 'Image URL' | |
46 | } | |
47 | ||
48 | widgets = { | |
49 | 'album_name': forms.TextInput( | |
50 | attrs={ | |
51 | 'placeholder': 'Add Album Name...' | |
52 | } | |
53 | ), | |
54 | 'artist': forms.TextInput( | |
55 | attrs={ | |
56 | 'placeholder': 'Add Artist...' | |
57 | } | |
58 | ), | |
59 | 'description': forms.Textarea( | |
60 | attrs={ | |
61 | 'placeholder': 'Add Description...' | |
62 | } | |
63 | ), | |
64 | } | |
65 | ||
66 | ||
67 | -------------------------------------------------------------------------------------- | |
68 | # Hide password: ******** | |
69 | class ProfileBaseForm(forms.ModelForm): | |
70 | class Meta: | |
71 | model = Profile | |
72 | fields = ["email", "age", "password", ] | |
73 | # Password input(hidden). | |
74 | widgets = { | |
75 | 'password': forms.PasswordInput(), | |
76 | } | |
77 | ||
78 | ||
79 | -------------------------------------------------------------------------------------- | |
80 | # Form methods: | |
81 | ||
82 | if request.method == 'GET': | |
83 | form = SomeForm() | |
84 | else: | |
85 | form = SomeForm(request.POST) | |
86 | if form.is_valid(): | |
87 | form.save() | |
88 | return redirect('index') | |
89 | ||
90 | context = {'form': form, } | |
91 | ||
92 | ||
93 | -------------------------------------------------------------------------------------- | |
94 | # Choice Enum Mixin: | |
95 | ||
96 | class ChoicesEnumMixin: | |
97 | @classmethod | |
98 | def choices(cls): | |
99 | return [(x.name, x.value) for x in cls] | |
100 | ||
101 | @classmethod | |
102 | def max_len(cls): | |
103 | return max(len(name) for name, _ in cls.choices()) | |
104 | ||
105 | ||
106 | class Gender(ChoicesEnumMixin, Enum): | |
107 | male = 'Male' | |
108 | female = 'Female' | |
109 | DoNotShow = 'Do not show' | |
110 | ||
111 | ||
112 | # in model: | |
113 | gender = models.CharField( | |
114 | choices=Gender.choices(), | |
115 | max_length=Gender.max_len(), | |
116 | ) | |
117 | ||
118 | ||
119 | -------------------------------------------------------------------------------------- |