每次在django中上传图像时,都会存储两张图像? [英] two copies of images are stored each time I upload an image in django?

查看:156
本文介绍了每次在django中上传图像时,都会存储两张图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我上传图片时,我看到它在我的项目中上传了两次。这两个位置是
/ Users / myproject / media / / Users / myproject / media / assets / uploaded_files / username / code>。我希望图像仅上传到后者。为什么要上传两个副本以及如何避免这种情况?

when I upload an image, i see it uploaded twice in my project. The two locations are /Users/myproject/media/ and /Users/myproject/media/assets/uploaded_files/username/. I expect the image to be uploaded only to the latter. why two copies are uploaded and how to avoid it?

settings.py:

MEDIA_URL="/media/"

MEDIA_ROOT = '/Users/myproject/media/'

这里是models.py

Here is models.py

UPLOAD_FILE_PATTERN="assets/uploaded_files/%s/%s_%s"

def get_upload_file_name(instance, filename):
    date_str=datetime.now().strftime("%Y/%m/%d").replace('/','_')
    return UPLOAD_FILE_PATTERN % (instance.user.username,date_str,filename)

class Item(models.Model):
    user=models.ForeignKey(User)
    price=models.DecimalField(max_digits=8,decimal_places=2)
    image=models.ImageField(upload_to=get_upload_file_name, blank=True)
    description=models.TextField(blank=True)

编辑:
我正在使用formwizards。以下是views.py:

I am using formwizards. Here is the views.py:

class MyWizard(SessionWizardView):
    template_name = "wizard_form.html"
    file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT))
    #if you are uploading files you need to set FileSystemStorage
    def done(self, form_list, **kwargs):
        for form in form_list:
           print form.initial
        if not self.request.user.is_authenticated():
            raise Http404
        id = form_list[0].cleaned_data['id']
        try:
            item = Item.objects.get(pk=id)
            print item
            instance = item
        except:
            item = None
            instance = None
        if item and item.user != self.request.user:
            print "about to raise 404"
            raise Http404
        if not item:
            instance = Item()
            for form in form_list:
                for field, value in form.cleaned_data.iteritems():
                    setattr(instance, field, value)
            instance.user = self.request.user
            instance.save()
        return render_to_response('wizard-done.html', {
            'form_data': [form.cleaned_data for form in form_list], })


def edit_wizard(request, id):
    #get the object
    item = get_object_or_404(Item, pk=id)
    #make sure the item belongs to the user
    if item.user != request.user:
        raise HttpResponseForbidden()
    else:
        #get the initial data to include in the form
        initial = {'0': {'id': item.id,
                         'price': item.price,
                         #make sure you list every field from your form definition here to include it later in the initial_dict
        },
                   '1': {'image': item.image,
                   },
                   '2': {'description': item.description,
                   },
        }
        print initial
        form = MyWizard.as_view([FirstForm, SecondForm, ThirdForm], initial_dict=initial)
        return form(context=RequestContext(request), request=request)


推荐答案

根据 docs ,您需要自己清理临时图像,这是您正在发生的事情。

According to the docs, you need to clean up the temporary images yourself, which is what's happening to you.

这是一个问题刚被合并成master和backported。完成所有成功的处理后,您可以尝试调用 storage.reset

Here's an issue that was just merged into master and backported. You can try calling storage.reset after finishing all of the successful processing.

这篇关于每次在django中上传图像时,都会存储两张图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆