How to not render django image field currently and clear stuff?

前端 未结 3 1433
我寻月下人不归
我寻月下人不归 2021-02-01 05:04

I took a look at following SO question, but had no luck. I don\'t know, maybe I didn\'t understand the answers.

1) How to remove the “Currently” tag and link of a FileIn

3条回答
  •  旧巷少年郎
    2021-02-01 05:31

    @mtndesign, you might also want a "remove" option, which you can place wherever you like in your template.

    class MyForm(forms.ModelForm):
        photo = forms.ImageField(required=False, widget=forms.FileInput)
        remove_photo = forms.BooleanField(required=False)
    
        ...
    
        def save(self, commit=True):
            instance = super(MyForm, self).save(commit=False)
            if self.cleaned_data.get('remove_photo'):
                try:
                    os.unlink(instance.photo.path)
                except OSError:
                    pass
                instance.photo = None
            if commit:
                instance.save()
            return instance
    

提交回复
热议问题