使用 Django,我正在创建使用标准 ImageField 上传的图像的缩略图,在通常的图像文件字段旁边的单独字段“缩略图”中添加表的路径。我正在尝试如何访问缩略图路径,在该路径中使用表单集在自定义模板中呈现图像字段对象,以便我可以显示它。
我想我需要在 inlineformset_factory 中添加一个“form=CarImageForm”,然后修改我的 forms.py,但我无法弄清楚如何去做,甚至这种方法是否正确。为了清楚起见,我没有在下面的代码示例中包含我这样做的尝试。
我的最终目标是返回链接到原始图像的缩略图图像 - 已经通过 Imagefile 字段显示。
提前致谢!
模板是:
<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset.forms %}
{% for field in form %}
{{ field.label }}: {{ field }}<br>
{% endfor %}
{% endfor %}
<p><input type="submit" value="Enter"/></p>
表格.py:
class CarForm(ModelForm):
class Meta:
model = Car
exclude = ['owner', 'uploaded']
视图.py:
# Edit an existing record
@login_required
def edit_existing(request, object_id=False):
try:
car = Car.objects.get(pk=object_id)
except Car.DoesNotExist:
raise Http404
ImageFormSet = inlineformset_factory(Car, CarImage, extra=1, max_num=1)
if request.method == 'POST':
form = forms.CarForm(request.POST, instance=car)
formset = ImageFormSet(request.POST, request.FILES, instance=car)
if formset.is_valid() and form.is_valid():
# Handle form.save() to include user id
new_car = form.save(commit=False)
new_car.owner = request.user
new_car.save()
# Formset - contains the attached images
formset.save()
return HttpResponseRedirect(new_car.get_absolute_url())
else:
form = forms.CarForm(instance=car)
formset = ImageFormSet(instance=car)
return render_to_response('edit_existing.html',
{'form': form, 'formset': formset},
context_instance=RequestContext(request))
模型.py:
class Car(models.Model):
make = models.CharField(max_length=64)
model = models.CharField(max_length=64)
owner = models.ForeignKey(User,editable=False)
uploaded = models.DateField(default=datetime.date.today,editable=False)
def get_absolute_url(self):
return reverse('vehicle_admin.views.car_detail', args=[str(self.id)])
def orig_car_id_folder(instance, filename):
return 'uploads/images/orig/{0}/{1}'.format(instance.car_id, filename)
def thumb_car_id_folder(instance, filename):
return 'uploads/images/thumb/{0}/{1}'.format(instance.car_id, filename)
class CarImage(models.Model):
car = models.ForeignKey(Car)
imagefile = models.ImageField(upload_to=orig_car_id_folder)
thumbnail = models.ImageField(upload_to=thumb_car_id_folder, editable=False)
# PIL tips from
# https://snipt.net/danfreak/generate-thumbnails-in-django-with-pil/
# http://www.mechanicalgirl.com/post/image-resizing-file-uploads-doing-it-easy-way/
def save(self):
import os
from PIL import Image
from cStringIO import StringIO
from django.core.files.uploadedfile import SimpleUploadedFile
THUMBNAIL_SIZE = (75, 75)
super(CarImage, self).save() # Use the commit=False param here?
image = Image.open(self.imagefile.path)
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
temp_handle = StringIO()
image.save(temp_handle, 'png')
temp_handle.seek(0)
name_ext = os.path.splitext(os.path.split(self.imagefile.name)[-1])
suf = SimpleUploadedFile(name_ext[0],
temp_handle.read(), content_type='image/png')
self.thumbnail.save(suf.name+'.png', suf, save=False)
super(CarImage, self).save()