2014-06-12 3 views
0

이미지를 업로드하면 내 프로젝트에서 두 번 업로드 된 것을 확인할 수 있습니다. 두 위치는 /Users/myproject/media//Users/myproject/media/assets/uploaded_files/username/입니다. 이미지가 후자에게만 업로드되기를 기대합니다. 왜 두 권의 사본이 업로드되고 그것을 피하는가? 여기 settings.py:장고에 이미지를 업로드 할 때마다 두 장의 이미지가 저장됩니까?

MEDIA_URL="/media/" 

MEDIA_ROOT = '/Users/myproject/media/' 

에서

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) 

편집 models.py입니다 : 내가 formwizards을 사용하고 .

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) 
+0

이미지를 전달하는보기 코드를 추가 할 수 있습니까? – schillingt

+0

@schillingt : 위의 views.py를 붙여 넣었습니다. 난보기에서 formwizard를 사용하고 있습니다. 위의 마법사 클래스에 file_storage가 있다는 것을 알았습니다. 아마도 그 원인이 될 수 있습니다. 마법사 클래스는 파일 저장을 요구합니다. 그렇지 않으면 파일이 저장되지 않습니다. –

답변

0

docs에 따르면, 임시 이미지를 직접 정리해야, 당신에게 무슨 일이 일어나고 있는지 다음은 views.py입니다.

다음은 issue이며 방금 마스터와 병합되었습니다. 모든 성공적인 처리를 마친 후에 storage.reset으로 전화 해 볼 수 있습니다.

+0

마법사가 성공적으로 완료되지 않으면 파일을 제거해야한다고 Doc는 말합니다. 그럴 경우 파일이 제거됩니다. – Rohan

+0

당신이 링크 된 문서에 storage.reset 핸들러가 보이지 않습니다. Rohan은 마법사가 성공적으로 완료되면 필요 없다고 언급합니다. –

+0

@Rohan : 마법사가 성공적으로 완료되었습니다. 관리자로 로그인하여 저장된 모델을 볼 수 있습니다. 편집보기를 위해 마법사를로드 할 때 관련된 문제가 있습니다. 추가 정보 : http://stackoverflow.com/questions/24173367/form-wizard-initial-data-for-edit-not-loading-properly-in-django –