整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

让Django ModelForm的ImageFiled显示为缩略图

认情况下,django的ModelForm显示的是图片的路径,其实并不友好。那么现在来修改一下,让它显示为一张缩略图:

在此之前,先打开源文件中的 widgets.py (site-packages/django/forms/widgets.py)看一下:

class ClearableFileInput(FileInput):
    clear_checkbox_label = _('Clear')
    initial_text = _('Currently')
    input_text = _('Change')
    template_name = 'django/forms/widgets/clearable_file_input.html'

看template_name的值,打开看一下:

{% if widget.is_initial %}{{ widget.initial_text }}: <a href="{{ widget.value.url }}">{{ widget.value }}</a>{% if not widget.required %}
<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}">
<label for="{{ widget.checkbox_id }}">{{ widget.clear_checkbox_label }}</label>{% endif %}<br>
{{ widget.input_text }}:{% endif %}
<input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>

好像看不太懂,但又能看懂一部分,能看出来这就是imagefiled 的html代码,现在将内容拷贝一下,创建一个自己的widgets, 在template文件夹中创建widget文件夹并在其中创建文件 ImageField.html, 将内容拷贝进去,并依葫芦画瓢改一下:

{% if widget.is_initial %}{{ widget.initial_text }}: <img src="{{ widget.value.url }}" width="100" height="50"/>{% if not widget.required %}
<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}" lay-skin="primary">
<label for="{{ widget.checkbox_id }}">{{ widget.clear_checkbox_label }}</label>{% endif %}<br>
{{ widget.input_text }}:{% endif %}
<input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>

在应用中创建文件 ImageWidget.py 文件(可以放在应用中的任何包中)

from django.forms.widgets import ClearableFileInput


class ImageWidget(ClearableFileInput):
    template_name = 'widget/ImageField.html'
    tempate_with_clear = ''

继承ClearableFileInput, template_name 改成刚创建的html模版.

在model form中使用:

class NodesForm(ModelForm):
    class Meta:
        model = Nodes
        exclude = ('site','order_num', 'node_path', 'node_level', 'node_group')
        # 如果有很多image字段,可以这样
        widgets_fields_dict = {k: ImageWidget for k in ("picture", "face_picture", "icon", 'icon_sel', "image1", 'image2', 'image3', 'image4')}
        widgets = widgets_fields_dict

下面看看效果:

件简介:

Imagine – 适用于 Windows 的图像浏览器

该版特点:

– 简体中文语言跟进
– 帮助文翻译为件简体中文
– 快捷键设置优化(鼠标中键切换模式)
– 集成多个增强格式插件(具体可看下面:集成插件)
– 重新制作安装包为 32Bit&64Bit 二合一版本
– 语言精简(保留韩文、简体中文、繁体中文)
– 还有更多强大的功能可使用插件实现
(https://www.nyam.pe.kr/dev/imagine/)

集成插件

数码相机原始图像格式插件(3FR、ARW、CR2、CRW、DCR、DNG、ERF、KDC、MOS、MRW、NEF、ORF、PEF、RAF、RAW、SRW、SR2、X3F)
JBIG 格式插件(JBG、JBIG)
AVIF 格式插件(AVIF、AVIFS)
Google WebP 格式插件(WEBP)
SVG 可扩展矢量图形图像格式插件(SVG、SVGZ)

软件特点:

– 查看并保存图像/动画文件
– 支持众多图像/动画文件格式:
PNG,JPG,GIF,BMP,PCX,TGA,TIFF,ICO,CUR,LBM的,ANI,FLI,WMF,EMF等。
[collapse]- 从动画文件中提取单个/所有帧
– 显示图像/动画文件的详细信息
– 显示 EXIF 信息
– 显示或隐藏透明度
– 大量的压缩文件格式的支持:
ZIP, RAR, 7Z, ALZ, HV3, CBZ, CBR, CB7, ARJ, LZH, EGG, TAR, ISO, CAB, ETC。
– 64 位版本
– Unicode 版本
– 多语言支持:英语,中国,德语,西班牙语,俄罗斯,丹麦,韩国,日本等。
– 缩图浏览器
– 捕获屏幕
– 批量转换
– 幻灯片
– 平铺视图(检查无缝拼接的纹理,对于游戏开发商/游戏模组非常有用)
– 创建动画
– 创建多个页面的图像
– 操纵和编辑图像:
垂直翻转和水平
旋转左,右和自由旋转
灰度
负片
更改颜色深度(1,4,8,16,24和32位)
交换颜色
调整每个像素,百分比或标准尺寸。
几个可用的重采样过滤器。
效过滤器,包括一个方便的预览浏览器
新增,保存和编辑调色板
设置和删除的透明度
还有更多…
– 可定制的快捷键为键盘和鼠标
– 命令行参数的支持
– 外壳扩展支持
– 添加插件的支持
– Total Commander 列表和压缩插件
– 不写不必要的注册表(基于INI文件的配置)
– 完全优化的二进制代码(微小,纯净,快速,整齐)[/collapse]

更新日志:

v1.1.5,2022 年 8 月 5 日
– 新增:支持高 DPI

下载地址:

城通盘:https://url61.ctfile.com/d/12483661-24755207-68db44 访问码:3333
123 盘:https://www.123pan.com/s/QdiA-y5pmh.html
天翼云:https://cloud.189.cn/t/2YrQF3VB7f2i
蓝奏云:https://www.lanzoux.com/b00ztqb2j 密码:aqpp

截图预览:

安装界面

浏览模式

查看模式