【问题标题】:django: Unable to get sorl-thumbnail workingdjango:无法让 sorl-thumbnail 工作
【发布时间】:2021-05-09 08:45:42
【问题描述】:

我正在创建一个用户可以上传图像的 django 站点,并且我想使用 sorl-thumbnail 来生成和缓存缩略图。我在 Fedora silverblue 主机上使用基于容器的工作流。

我已经设置了一个 memcached 缓存引擎(使用 memcached docker 映像),并且可以毫无问题地在 django-shell 中设置和获取缓存中的值。我已运行迁移命令,并将 sorl-thumbnail 添加到我的已安装应用程序中。我已经运行了 ./manage.py createcachetable 命令并且没有错误。我正在使用 pylibmc:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

我创建了一个具有 sorl.thumbnail ImageField 的模型,尽管我希望最终使用标准的 imagefield,我相信这是可能的。

我有以下模型、视图和模板:

型号...

class Image(models.Model):
    image_file = ImageField(upload_to=user_directory_path)
    #thumbnail = models.ImageField(upload_to=str(user_directory_path) + '_thumb', null=True)
    userprofile = models.ForeignKey(ForumProfile, on_delete=models.CASCADE, related_name="images")

查看...(此问题调试时添加get功能)...

class ForumProfileUploadView(LoginRequiredMixin, FormView):
    form_class = ImageForm
    template_name = 'list.html'
    success_url = reverse_lazy('my-view')

    def get(self, request, *args, **kwargs):
        form = self.form_class()
        message = 'Hi!'
        images = Image.objects.all()
        context = {'images': images, 'form': form, 'message': message}
        return render(self.request, 'list.html', context)

    def form_valid(self, form):
        obj = form.save(commit=False)
        obj.userprofile = self.request.user.profile.forumprofile
        # img = PillowImage.open(obj.image_file.file)
        # obj.thumbnail = MakeThumbnail(self.request.FILES['image_file'])
        # breakpoint()
        obj.save()
        message = 'Success!'
        images = Image.objects.all()
        breakpoint()
        context = {'images': images, 'form': form, 'message': message}
        return render(self.request, 'list.html', context)

    def form_invalid(self, form):
        message = 'The form is not valid. Fix the following error:'
        images = Image.objects.all()
        context = {'images': images, 'form': form, 'message': message}
        return render(self.request, 'list.html', context)

模板...

<!DOCTYPE html>
<html lang='en'>
    <head>
    </head>
    <body>

        <!-- List of uploaded documents -->
    {% block content %}
        {% load thumbnail %}
        {% if images %}
            All images in the database:
                {% for image in images %}
                        {% thumbnail image.image_file.url "100x100" as im %}
                            <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
                        {% endthumbnail %}
                {% endfor %}
        {% else %}
            <p>No images.</p>
        {% endif %}

        <!-- Upload form. Note enctype attribute! -->
        <form action="{% url 'my-view' %}" method="post" enctype="multipart/form-data">
            {% csrf_token %}
            {{ message }}
            <p>{{ form.non_field_errors }}</p>

            <p>{{ form.image_file.label_tag }} {{ form.image_file.help_text }}</p>

            <p>
                {{ form.image_file.errors }}
                {{ form.image_file }}
            </p>

            <p><input type="submit" value="Upload"/></p>
        </form>
    {% endblock content %}
</body>

【问题讨论】:

    标签: python django sorl-thumbnail podman


    【解决方案1】:

    我已经设法让它工作了。我相当肯定,必要的事情是让我将缓存设置为适用于整个站点,尽管我可以将它用于特定视图,这允许 sorl.thumbnail 开始工作。 https://docs.djangoproject.com/en/3.1/topics/cache/#the-per-site-cache

    【讨论】:

      猜你喜欢
      • 2011-05-20
      • 2016-05-09
      • 2015-11-02
      • 2012-09-05
      • 2023-04-08
      • 2012-06-21
      • 2011-05-13
      • 2012-11-22
      • 2013-01-30
      相关资源
      最近更新 更多