Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Django Basics Exam Helper
- --------------------------------------------------------------------------------------
- # How-to-add-class-and-other-atrributes-to-form-fields-in-django ->
- # https://www.letscodemore.com/blog/how-to-add-class-and-other-atrributes-to-form-fields-in-django/
- --------------------------------------------------------------------------------------
- # Django form field types: Widgets, options and validations ->
- # https://www.webforefront.com/django/formfieldtypesandvalidation.html
- --------------------------------------------------------------------------------------
- # How to setup Django project from 0 ->
- # https://pastebin.com/V5a9GanA
- --------------------------------------------------------------------------------------
- --------------------------------------------------------------------------------------
- # Disable fields and delete:
- class AlbumDeleteForm(AlbumBaseForm):
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.__disable_fields()
- def save(self, commit=True):
- if commit:
- self.instance.delete()
- return self.instance
- def __disable_fields(self):
- for field in self.fields.values():
- field.widget.attrs['disabled'] = True
- field.widget.attrs['readonly'] = True
- field.required = False
- --------------------------------------------------------------------------------------
- # BaseForm ,labels and placeholders:
- class AlbumBaseForm(forms.ModelForm):
- class Meta:
- model = AlbumModel
- fields = '__all__'
- labels = {
- 'image_url': 'Image URL'
- }
- widgets = {
- 'album_name': forms.TextInput(
- attrs={
- 'placeholder': 'Add Album Name...'
- }
- ),
- 'artist': forms.TextInput(
- attrs={
- 'placeholder': 'Add Artist...'
- }
- ),
- 'description': forms.Textarea(
- attrs={
- 'placeholder': 'Add Description...'
- }
- ),
- }
- --------------------------------------------------------------------------------------
- # Hide password: ********
- class ProfileBaseForm(forms.ModelForm):
- class Meta:
- model = Profile
- fields = ["email", "age", "password", ]
- # Password input(hidden).
- widgets = {
- 'password': forms.PasswordInput(),
- }
- --------------------------------------------------------------------------------------
- # Form methods:
- if request.method == 'GET':
- form = SomeForm()
- else:
- form = SomeForm(request.POST)
- if form.is_valid():
- form.save()
- return redirect('index')
- context = {'form': form, }
- --------------------------------------------------------------------------------------
- # Choice Enum Mixin:
- class ChoicesEnumMixin:
- @classmethod
- def choices(cls):
- return [(x.name, x.value) for x in cls]
- @classmethod
- def max_len(cls):
- return max(len(name) for name, _ in cls.choices())
- class Gender(ChoicesEnumMixin, Enum):
- male = 'Male'
- female = 'Female'
- DoNotShow = 'Do not show'
- # in model:
- gender = models.CharField(
- choices=Gender.choices(),
- max_length=Gender.max_len(),
- )
- --------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement