1

FileField와 비슷한 AWS S3에 저장하는 ImageField가 있습니다. 양식에는 이미지 파일 경로를 나타내는 "현재"레이블이 있습니다. 트림하고 파일 이름 만 표시하고 싶습니다.ImageField/FileField Django form 현재 파일 이름에 대한 경로를자를 수 없습니다.

Django : customizing FileField value while editing a model의 최신 답변을 참조해도, 나는 여전히 작동하지 않습니다. 그것은이 같은 파일 경로 이름으로 "현재"이 표시

: https://imgur.com/a/xkUZi

form.py

class CustomClearableFileInput(ClearableFileInput): 
    def get_template_substitution_values(self, value): 
     """ 
     Return value-related substitutions. 
     """ 
     logging.debug("CustomClearableFileInput %s",value) <-- it never came here 
     return { 
      'initial': conditional_escape(path.basename(value.name)), 
      'initial_url': conditional_escape(value.url), 
     } 

class CompanySettingEdit(forms.ModelForm): 
    display_companyname = forms.CharField(max_length=50, required=True)  
    company_logo = forms.ImageField(widget=CustomClearableFileInput) 

    class Meta: 
     model = Company 
     fields = ("display_companyname","company_logo") 

model.py

class Company(models.Model): 
    display_companyname = models.CharField(max_length=50)  
    company_logo = models.ImageField(upload_to=upload_to('company_logo/'), blank=True, null=True, storage=MediaStorage()) 

어떻게 이런 일이있을 수 : 현재 : filename.jpg

FYI - Imag eField/FileField, 나는 그 두 가지 모두를 시도해 보았습니다. 장고 사용 == 1.11.7

+0

'CustomClearableFileInput'도 공유 할 수 있습니까? – DhiaTN

+0

죄송합니다, 그것을 당신의 코드와 동일하게 설정하는 것을 잊었습니다. 차이점은 메타입니다. "company_logo"필드가 있습니다. 그것은 차이를 만들어야합니다. CustomClearableFileInput으로 디버깅, 그건 그 기능에 들어간 적이 없어. 전화 할 때 잘못된 것이 있습니까? – Axil

+0

사용중인 Django 버전은 무엇입니까? – DhiaTN

답변

1

장고 1.11.x에서 get_template_substitution_values은 더 이상 사용되지 않습니다. CustomClearableFileInput의 새 구현은 다음과 같습니다.

class CustomClearableFileInput(ClearableFileInput): 
    def get_context(self, name, value, attrs): 
     value.name = path.basename(value.name) 
     context = super().get_context(name, value, attrs)  
     return context 
+0

을 사용하여 작동합니다. https://imgur.com/a/0NEgu, 한 가지 문제가 있지만 링크가 올바르지 않습니다. 이미지 URL을 가리킬 때 현재 페이지 (http : // localhost : 8000/employee/companysettings /)를 가리 킵니다 (https://test-static.s3.amazonaws.com/media/company_logo/와 같은 것). 15/littlepony.jpeg) – Axil

+0

내 답변을 업데이트했습니다! – DhiaTN

+0

그 원래의 문제와 동일합니다. 그것은 https://imgur.com/a/qVF2i를 손질하지 않았습니다. – Axil