我有一个模型和一个表单(forms.ModelForm),其中有一个 ImageField。该模型类似于:
class Business(models.Model):
business_name = models.CharField(max_length=128)
.
.
business_image = ImageField(
max_length=128,
upload_to=upload_business_image_handler,
height_field='business_image_height',
width_field='business_image_width'
)
business_image_height = models.IntegerField(blank=True, null=True)
business_image_width = models.IntegerField(blank=True, null=True)
.
.
随附表格:
class BusinessForm(forms.ModelForm):
def __init__(self, data=None, files=None, branch_list = None, country_list = None, *args, **kwargs):
super(BusinessForm, self).__init__(data, *args, **kwargs)
# create the MultipleChoiceField choice_list based on language
self.fields['branch'].choices = branch_list
self.fields['country'].choices = country_list
self.postdata = data
self.files = files
business_name = forms.CharField(widget=forms.TextInput(attrs={'size':'50', 'class': 'address'}), required=True)
#business_image = forms.ImageField(widget=forms.ClearableFileInput())
表单中的最后一行被注释掉,因为 forms.ClearableFileInput() 是 FileFields 和 ImageFields 的默认小部件。
现在,当使用此表单编辑现有记录时,模板中的图像如下所示:
Currently: <image_url>
Change: <browse_button>
我想更改文本标签“当前”和“更改”,而不是显示 image_url 我想显示图像。
当然,我可以复制和修改在“site-packages/django/forms/widgets.py”中找到的“class ClearableFileInput(FileInput)”的代码,但我想知道这是否是实现此目的的方法。
任何帮助或建议表示赞赏。
现在我查看了'class ClearableFileInput(FileInput)'的代码