【问题标题】:Many files upload in ImageField form - django许多文件以 ImageField 形式上传 - django
【发布时间】:2010-11-21 21:50:02
【问题描述】:

我的问题很简单。我有这样的模板:

<form enctype="multipart/form-data"
action="{% url offers.views.add_offer %}" method="post">    
    <input type="file" name="image1" />
    <input type="file" name="image2" />     
    <input type="submit" value="Add" />
</form>

模型看起来像这样:

class Image(models.Model):
    image = models.ImageField(upload_to='uploads/images/offers/')

还有这样的形式(它使用模型图像):

class ImageForm(ModelForm):
    class Meta:
        model = Image

然后这样看:

    for f in request.FILES:
    # imageform:
        image = ImageForm(request.POST, f)
        image.save()

问题是我无法上传图片。我想将图像保存在两个单独的实例 od Image Model 中。
我有一个错误:

'unicode' 对象没有属性 'get'

感谢您的帮助和回复。

更新以提供更多信息

【问题讨论】:

    标签: django django-models upload


    【解决方案1】:

    伙计,你需要 Django Formsets:

    http://docs.djangoproject.com/en/dev/topics/forms/formsets/

    已编辑

    观点:

    def manage_images(request):
        ImageFormSet = formset_factory(ImageForm)
        if request.method == 'POST':
            formset = ImageFormSet(request.POST, request.FILES)
            if formset.is_valid():
                # do something with the formset.cleaned_data
        else:
            formset = ImageFormSet()
        return render_to_response('manage_images.html', {'formset': formset})
    

    模板:

    <form enctype="multipart/form-data" action="{% url offers.views.add_offer %}" method="post">
        {{ formset.management_form }}
        <table>
            {% for form in formset.forms %}
            {{ form }}
            {% endfor %}
        </table>
    </form>
    

    【讨论】:

    • 但是我必须如何使用它呢?我的问题是从我在想的 request.FILES 中获取数据。你能给我举个例子,如何在许多带有表单集的实例中将许多文件保存到同一个模型中吗?
    【解决方案2】:

    您可以在此处找到file upload 的文档。

    我将图像保存在表单的 save() 方法中,如下所示:

    def save(self): 
        if self.cleaned_data.get('galleryname'):
            if self.cleaned_data.get('images1'):
    
                path = 'images/'+ urlify(self.cleaned_data.get('galleryname'))+self.cleaned_data.get('images1').name
                destination = open(s.MEDIA_ROOT+path, 'wb+')
                for chunk in self.cleaned_data.get('images1').chunks():
                    destination.write(chunk)
                p = Photo()
                p.picture="./"+path
                p.save()
    

    在我看来

    form = CompleteSubscriptionForm(request.POST, request.FILES, error_class=DivErrorList)
    if form.is_valid(): # All validation rules pass
            form.save()
    

    【讨论】:

      【解决方案3】:

      是什么让您认为这会奏效?您正在遍历 request.FILES 并尝试在每次迭代时实例化一个表单,并传递一个文件对象。这与文档中的内容完全不同,它告诉您将整个 request.FILES 传递进去。

      评论后编辑看,你没有给我们太多的信息。您的模型有一张或两张图片吗?你为什么要分别处理这两个图像?您是要创建两个单独的模型实例,还是一个包含两个图像?你到底想做什么?

      基本上你只想这样做:

      form = ImageForm(request.POST, request.FILES)
      if form.is_valid():
          form.save()
      

      就是这样。

      【讨论】:

      • 那我必须做什么?你有什么解决办法吗?
      • Model 看起来像这样: class Image(models.Model): image = models.ImageField(upload_to='uploads/images/offers/') 它只有一个 ImageField。我想创建同一模型的两个单独的实例。我知道如何保存一个文件/图像。就像你上面描述的那样简单。这对我来说不是问题。我的问题是我不知道如何创建这两个实例(从 request.FILES 中检索文件,然后将其保存到两个单独的新实例中)。如果您想了解更多信息,我可以发布它。我想把它保存到同一个模型中。
      猜你喜欢
      • 2011-04-28
      • 2014-06-04
      • 2012-12-11
      • 2016-10-26
      • 1970-01-01
      • 2013-11-06
      • 2019-06-27
      • 2013-02-14
      • 2016-03-15
      相关资源
      最近更新 更多