当前位置:主页 > 软件编程 > Python代码 >

django ModelForm修改显示缩略图 imagefield类型的实例

时间:2021-04-21 09:37:25 | 栏目:Python代码 | 点击:

在使用django的modelform的时候,修改表单,图片在form表单显示的是一个链接。显示缩略图如下

第一步:

from django.forms.widgets import ClearableFileInput
 
 
class ImageWidget(ClearableFileInput):
 
  template_with_initial = (
    '%(initial_text)s: <a href="%(initial_url)s" rel="external nofollow" ><img width="100px" height="100px" src="%(initial_url)s"></a> '
    '%(clear_template)s<br />%(input_text)s: %(input)s'
  )
 
  template_with_clear = ''

图片的form组件使用

ClearableFileInput

继承它,重写里面一个属性。

第二步:使用这个样式组件

class XXXForm(forms.ModelForm):
 
  class Meta:
    model = XXX
    exclude = ['XXX', 'is_XXX']
    widgets = {'backend_image':ImageWidget}

您可能感兴趣的文章:

相关文章