1

使用 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()
4

2 回答 2

0

我认为这是我的一个基本疏忽。

我的问题是在我的 CarImage 模型中为“缩略图”字段设置 editable=False。这对于初始数据提交来说很好,但会导致在编辑条目时出现问题,因为该字段会自动排除在表单中。(有关更多信息,请参阅我上面的代码片段。)

我的下一步是为我的 add_new 视图设置一个自定义表单(我最初没有包含此视图,但它是我用来启用数据初始上传的),如下所示:

class InitialCarImageForm(ModelForm):
    class Meta:
        model = CarImage
        exclude = ['thumbnail']

我想正确的方法是使用 editable=False 但除非我犯了一个严重的错误,否则它可能对我有用。

我是该网站的新手,所以请让我知道以这种方式回答我的问题是否是关闭它的正确方法。

于 2013-01-25T13:11:35.433 回答
0

如果你愿意尝试使用我的django-extra-views库,你可以这样做:

from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator

from extra_views import UpdateWithInlinesView, InlineFormSet


class CarForm(ModelForm):

    def __init__(self, *args, **kwargs):
        self.user = self.kwargs.pop('user')
        super(CarForm, self).__init__(*args, **kwargs)

    def save(self, commit=True)
        instance = super(CarForm, self).save(commit=False)
        instance.user = self.user

        if commit:
            instance.save()
        return instance

    class Meta:
        model = Car
        exclude = ['owner', 'uploaded']


CarImageInline(InlineFormSet):
    model = CarImage
    extra = 1
    max_num = 1


UpdateCarView(UpdateWithInlinesView):
    pk_url_kwarg = 'object_id'
    inlines = [CarImageInline]
    template_name = 'edit_existing.html'
    form_class = CarForm

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(UpdateCarView, self).dispatch(*args, **kwargs)

    def get_form_kwargs(self):
        kwargs = super(UpdateCarView, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs

    def get_success_url(self):
        return self.object.get_absolute_url()

主要区别:

  • 表单负责将用户分配给汽车,表单在实例化时会控制用户(请参阅 get_form_kwargs)。这样做的原因是,对于基于类的视图,覆盖 form_valid(或在本例中为 forms_valid)有点笨拙,所以我更喜欢将东西放入表单中。
  • 你得到的不是formset你的上下文,inlines而是一个表单集列表。这是因为从技术上讲,您可以拥有任意数量的内联表单集,即使在这种情况下您只有一个,它实际上与 Django 管理员的工作方式相同。
  • 覆盖dispatch应用login_required装饰器很笨重,幸运的是,它很容易抽象为 mixin,请参阅django-braces了解一些现成的。
于 2013-01-29T00:41:14.517 回答