如何修剪'来自GenericView的不必要输入字段?

发布于 2025-01-25 14:44:43 字数 2990 浏览 2 评论 0原文

在我当前的项目中,我正在创建一个小CMS,因此客户将能够在网站上使用新图像更新他的画廊并替换旧图像。为此,我使用通用视图来简化整个过程。

添加新图像没有问题,但是UpdateView提供了一些html元素,我不希望我的客户看到:

“在此处输入映像”

这是代码:

models.py.py

class GalleryPicture(models.Model):
title = models.CharField(max_length=200)
img = WEBPField(
    verbose_name=_('Image'),
    upload_to=image_folder,
    null=True,
    blank=True,
)

def __str__(self):
    return f'{self.title}'

def get_absolute_url(self):
    return reverse('Galerie Überblick')

class Meta:
    verbose_name_plural = 'GalleryPictures'

i我还使用自定义字段进行图像,因此所有上传的图片都将自动转换为WebP格式:

fields.py

class WEBPFieldFile(ImageFieldFile):

def save(self, name, content, save=True):
    content.file.seek(0)
    image = Image.open(content.file)
    image_bytes = io.BytesIO()
    image.save(fp=image_bytes, format="WEBP")
    image_content_file = ContentFile(content=image_bytes.getvalue())
    super().save(name, image_content_file, save)

class WEBPField(models.ImageField):
attr_class = WEBPFieldFile

urls.py

    path('content-manager/overview/gallery/<int:pk>/edit', EditGalleryImage.as_view(), name="Bild ändern")

views.pys.py

class EditGalleryImage(UpdateView):
  model = GalleryPicture
  template_name = './pages/content_edit/edit_gallery_image.html'
  fields = ['title', 'img',]

edit_gallery_image.html

    <div class="form-content">
    <form action="" method="POST" enctype="multipart/form-data">
            {% csrf_token %}
        
            {{ form }}

            <input class="submit-button link-button" type="submit" value="Speichern" name="save-image">
            <input class="submit-button link-button" type="submit" value="Löschen" name="delete-image">
    </form>
</div>

我的第一种方法是以形式创建自定义表单模型。py并为此HTML元素定义了一些类,因此我可以用JS:

forms.pys.pys.pys.pys.pys.pys.pys.pys.pys.pys.pys.py.

class UpdateImageForm(forms.ModelForm):
class Meta:
    model = GalleryPicture
    fields = ('title', 'img')
    widgets = {
        'img': forms.ImageField(
            widget=forms.FileInput(attrs={'class': 'nice'})
        ),
    }

但是如果我会使用我的 views.py 这样:

class EditGalleryImage(UpdateView):
  model = GalleryPicture
  template_name = './pages/content_edit/edit_gallery_image.html'
  form_class = UpdateImageForm

我会遇到一个错误:

AttributeError at /content-manager/overview/gallery/1/edit
'ImageField' object has no attribute 'is_hidden' 

对于此错误,我发现的修复程序在我的情况下也无济于事。

也许你们中的一个知道“修剪”这些HTML元素的更好方法或知道如何解决此错误?我会非常感谢每个答案。

In my current project i am creating a little CMS so the client will be able to update his gallery on the website with new images and replace the old ones. For this I am using the generic Views to simplify the whole process.

Adding a new image was no problem, but the UpdateView is providing some html elements, which I dont want my client to see:

enter image description here

Here is the code:

models.py

class GalleryPicture(models.Model):
title = models.CharField(max_length=200)
img = WEBPField(
    verbose_name=_('Image'),
    upload_to=image_folder,
    null=True,
    blank=True,
)

def __str__(self):
    return f'{self.title}'

def get_absolute_url(self):
    return reverse('Galerie Überblick')

class Meta:
    verbose_name_plural = 'GalleryPictures'

I am also using a custom field for images, so all the uploaded pictures would be automatically converted into WEBP format:

fields.py

class WEBPFieldFile(ImageFieldFile):

def save(self, name, content, save=True):
    content.file.seek(0)
    image = Image.open(content.file)
    image_bytes = io.BytesIO()
    image.save(fp=image_bytes, format="WEBP")
    image_content_file = ContentFile(content=image_bytes.getvalue())
    super().save(name, image_content_file, save)

class WEBPField(models.ImageField):
attr_class = WEBPFieldFile

urls.py

    path('content-manager/overview/gallery/<int:pk>/edit', EditGalleryImage.as_view(), name="Bild ändern")

views.py

class EditGalleryImage(UpdateView):
  model = GalleryPicture
  template_name = './pages/content_edit/edit_gallery_image.html'
  fields = ['title', 'img',]

edit_gallery_image.html

    <div class="form-content">
    <form action="" method="POST" enctype="multipart/form-data">
            {% csrf_token %}
        
            {{ form }}

            <input class="submit-button link-button" type="submit" value="Speichern" name="save-image">
            <input class="submit-button link-button" type="submit" value="Löschen" name="delete-image">
    </form>
</div>

My first approach was to create a custom form model in forms.py and define some classes there for this html elements, so I could hide them with JS:

forms.py

class UpdateImageForm(forms.ModelForm):
class Meta:
    model = GalleryPicture
    fields = ('title', 'img')
    widgets = {
        'img': forms.ImageField(
            widget=forms.FileInput(attrs={'class': 'nice'})
        ),
    }

But if I will use my views.py like this:

class EditGalleryImage(UpdateView):
  model = GalleryPicture
  template_name = './pages/content_edit/edit_gallery_image.html'
  form_class = UpdateImageForm

I get an Error:

AttributeError at /content-manager/overview/gallery/1/edit
'ImageField' object has no attribute 'is_hidden' 

and the fixes, which I have found for this error, were also not helpful in my case.

Maybe one of you knows the better approach for 'trimming' these HTML elements or knows how to solve this error? I would be very grateful for every answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写下不归期 2025-02-01 14:44:43

使用django脆皮形式使其可以呈现。我假设他们还可以为您提供对形式呈现的东西进行更多精细谷物控制的选项。

Use Django crispy forms to make it presentable. I would assume they would also provide you the option for more fine grain control on what is rendered with the form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文