django:无法使 sorl-thumbnail 工作

问题描述

我正在制作一个 django 网站,用户可以在其中上传图片,我想使用 sorl-thumbnail 生成缩略图和缓存。我在 Fedora silverblue 主机上使用基于容器的工作流,使用 podman。

我已经设置了一个 memcached 缓存引擎(使用 memcached docker 镜像),并且可以在 django-shell 中设置和获取缓存中的值,没有问题。我已经运行了 migrate 命令,并将 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,context)

    def form_invalid(self,form):
        message = 'The form is not valid. Fix the following error:'
        images = Image.objects.all()
        context = {'images': images,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>

解决方法

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