0

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?

In settings.py:

MEDIA_URL="/media/"

MEDIA_ROOT = '/Users/myproject/media/'

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)

EDIT: 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)
brain storm
  • 30,124
  • 69
  • 225
  • 393
  • Can you add your view code that's passing the image along? – schillingt Jun 12 '14 at 03:41
  • @schillingt: I have pasted the views.py above. I am using formwizard in the views. I noticed that I have file_storage in the wizard class above. That is probably causing it. but wizard class requires file storage, otherwise they don't function – brain storm Jun 12 '14 at 04:11

1 Answers1

0

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

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.

schillingt
  • 13,493
  • 2
  • 32
  • 34
  • Doc says you need to remove files when wizard does not complete successfully. If it does, files will be removed. – Rohan Jun 12 '14 at 04:24
  • I do not see any storage.reset handler in the docs you linked.and as Rohan mentions it is not needed if wizard completes successfully – brain storm Jun 12 '14 at 04:27
  • @Rohan: my wizard completes successfully. I could log in the admin and see the model saved. but I have a related issue when I load the wizard for edit view. More here : http://stackoverflow.com/questions/24173367/form-wizard-initial-data-for-edit-not-loading-properly-in-django – brain storm Jun 12 '14 at 04:28