1

I have a ImageField that stores in AWS S3 (similar to FileField). In the form, it has this "Currently" label that shows the image file path. I would like to trim and just show the filename.

referring to the latest answer in Django : customizing FileField value while editing a model, I still cant get it to work.

It shows "Currently" with the file path name like this: https://i.stack.imgur.com/WeSoi.jpg

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())

How can I have something like this: Currently: filename.jpg

FYI - ImageField / FileField, I tried both it doesnt make a difference. Im using Django==1.11.7

Dhia
  • 10,119
  • 11
  • 58
  • 69
Axil
  • 3,606
  • 10
  • 62
  • 136
  • Can you also share your `CustomClearableFileInput`? – Dhia Dec 19 '17 at 14:15
  • sorry forgot to put it, its the same as your code. difference is in Meta I had the fields "company_logo". Should that make a difference. I debug into CustomClearableFileInput, it never went into that function. Is there something wrong when calling it ? – Axil Dec 19 '17 at 23:09
  • Which Django version are you using!? – Dhia Dec 20 '17 at 01:08
  • Im using Django==1.11.7 – Axil Dec 20 '17 at 01:10

1 Answers1

3

In Django 1.11.x get_template_substitution_values is deprecated. New implementation of CustomClearableFileInput could be as follow:

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
Axil
  • 3,606
  • 10
  • 62
  • 136
Dhia
  • 10,119
  • 11
  • 58
  • 69
  • it works. https://imgur.com/a/0NEgu, but one problem, the link is not correct now. it points to the current page (http://localhost:8000/employee/companysettings/) when it should point to the image url (something like this https://test-static.s3.amazonaws.com/media/company_logo/15/littlepony.jpeg) – Axil Dec 20 '17 at 01:26
  • that didnt work, its the same as the original problem. it did not trim https://imgur.com/a/qVF2i. – Axil Dec 20 '17 at 02:02
  • i got it to work with value.name = path.basename(value.name). edited answer. thank you very much – Axil Dec 20 '17 at 02:14
  • It's dangerous to change the internal state of an object, but it seems to be the only work around here otherwise you will have to override HTML template of the widget. Well done! :+1: – Dhia Dec 20 '17 at 06:27
  • https://stackoverflow.com/questions/48314365/django-s3-trim-imagefield-filename-but-not-the-url-path @DhiaTN can you check my subsequent question, somehow its not complete the solution. – Axil Jan 18 '18 at 21:23
  • Editing `name` will not work since [url is generated using `name`](https://github.com/django/django/blob/aed89adad54a977829c4f180b036033e031ebcc7/django/db/models/fields/files.py#L60-L62) so the links will be invalid. The correct way is to use your own template. – vikki May 08 '19 at 13:30