Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Read-Only 1 way
- class ReadOnlyAlbumForm(ModelForm):
- class Meta:
- model = Album
- fields = ('name', 'artist_name', 'genre', 'description', 'image_url', 'price')
- widgets = {
- 'name': TextInput(attrs={'readonly': 'readonly'}),
- 'artist_name': TextInput(attrs={'readonly': 'readonly'}),
- 'genre': TextInput(attrs={'readonly': 'readonly'}),
- 'description': Textarea(attrs={'readonly': 'readonly'}),
- 'image_url': TextInput(attrs={'readonly': 'readonly'}),
- 'price': NumberInput(attrs={'readonly': 'readonly'}),
- }
- class DeleteAlbumView(views.DeleteView):
- queryset = Album.objects.all()
- template_name = 'albums/album-delete.html'
- success_url = reverse_lazy('index')
- form_class = ReadOnlyAlbumForm
- def get_form_kwargs(self):
- kwargs = super().get_form_kwargs()
- kwargs["instance"] = self.object
- return kwargs
- Read-only 2 way
- class ReadonlyViewMixin:
- def get_form(self, form_class=None):
- form = super().get_form(form_class=form_class)
- for field in form.fields.values():
- field.widget.attrs["readonly"] = "readonly"
- return form
- class DeleteAlbumView(ReadonlyViewMixin, views.DeleteView):
- queryset = Album.objects.all()
- template_name = "albums/album-delete.html"
- success_url = reverse_lazy("index")
- form_class = modelform_factory(
- Album,
- fields=("name", "artist_name", "genre", "description", "image_url", "price"),
- )
- def get_form_kwargs(self):
- kwargs = super().get_form_kwargs()
- kwargs["instance"] = self.object
- return kwargs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement