I am making a django site where users can upload images and I want to use sorl-thumbnail for thumbnail generation and caching. I am using a container based workflow using podman on a fedora silverblue host.
I have setup a memcached cache engine (using the memcached docker image), and can set and get values to and from the cache in django-shell with no issues. I have run the migrate command with sorl-thumbnail added to my installed-apps. I have run the./manage.py createcachetable command and no errors. I am using pylibmc with:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
'LOCATION': '127.0.0.1:11211',
}
}
I have created a model which has the sorl.thumbnail ImageField, although I hope to use a standard imagefield eventually, which I believe is possible.
I have the following model, view, and template:
model...
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")
view...(the get function is added during debugging this issue)...
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)
template...
<!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>
I have managed to get it working. I am fairly certain that the necessary thing was for me to set the cache to work for the entire site, although I could have used it for a specific view, and this allowed sorl.thumbnail to start working. https://docs.djangoproject.com/en/3.1/topics/cache/#the-per-site-cache
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.