gpt4 book ai didi

python - Django-Imagekit 不创建缩略图

转载 作者:太空宇宙 更新时间:2023-11-03 19:16:31 25 4
gpt4 key购买 nike

我希望 imagekit 在用户上传时将图片作为缩略图返回,并仍然保留原始图片。但缩略图应该是它将返回的图片。我读过 imagekit 文档,但没有成功。它不返回缩略图。它只会返回用户上传的图像的实际大小。我一直在寻找有关如何进行这项工作的教程,但我找不到深入研究该主题的教程。如何让 imagekit 以缩略图形式返回上传的图片?我正在使用 imagekit v2.0。

型号

       class Meek(models.Model):
user=models.ForeignKey(User)
title=models.CharField(max_length=250, unique=True)
address=models.CharField(max_length=200)
city=models.CharField(max_length=200)
state=models.CharField(max_length=200)
main_view=models.ImageField(upload_to="photos",blank=True, null=True)
side_view=models.ImageField(upload_to="photos",blank=True, null=True)
pub_date=models.DateTimeField()
thumbnail=ImageSpecField([Adjust(contrast=1.2, sharpness=1.1),ResizeToFill(50, 50)], image_field='main_view', format='JPEG',options={'quality': 90})
thumbnail=ImageSpecField([Adjust(contrast=1.2, sharpness=1.1),ResizeToFill(50, 50)], image_field='side_view', format='JPEG',options={'quality': 90})
def __unicode__(self):
return self.title

观看次数

            def findme(request):
extra_data_context={}
if request.method=="POST":
form=MeekForm(request.POST, request.FILES)
if form.is_valid():
data=form.cleaned_data
newmeeks=Meek(
user=request.user,
pub_date=datetime.datetime.now(),
title=data['title'],
main_view=request.FILES['main_view'],
side_view=request.FILES['side_view'],
address=data['address'],
city=data['city'],
state=data['state'])
newmeeks.save()
extra_data_context.update({'MeekForm':form})
else:
form = MeekForm()
extra_data_context.update({'MeekForm':form})
extra_data_context.update({'Meeks':Meek.objects.filter(user=request.user)})
return render_to_response('postme.html',extra_data_context,context_instance=RequestContext(request))

模板

       {% for Meek in Meeks.object_list %}
{# posted only this because, I'm only having problem with images. #}
<p><img src="{{MEDIA_URL}}/{{Meek.main_view}}"/> <img src="{{MEDIA_URL}}/{{Meek.side_view}}"/> </p>

{% endfor %}

最佳答案

这对我有用:

型号:

class Model(models.Model):
image_1 = ProcessedImageField([Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(500, 370)], upload_to=generate_image_filename_1, format='JPEG', options={'quality': 90})
thumbnail_1 = ImageSpec([Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(83, 78)], image_field='image_1', cache_to=generate_cache_filename, format='JPEG', options={'quality': 90})

查看:

def thumbnail(request, pk):
thumb_pk = pk
instance = Model.objects.get(pk=thumb_pk)
return render_to_response('thumbnail.html',{'instance': instance},context_instance = RequestContext(request))

模板“thumbnail.html”:

# show the tumbnail

<img alt="thumbnail" src="{{ instance.thumbnail_1.url }}" width="83" height="78">

关于python - Django-Imagekit 不创建缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11191863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com