1

我希望 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 %}
4

1 回答 1

2

这对我有用:

模型:

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">
于 2012-06-25T19:33:02.897 回答