wagtailze markdown如何支持img标签

在wagtail的markdown里添加图片有点不是很方便,主要是尺寸不好灵活的更改。那么,怎么直接用HTML的img标签来处理图片呢?

avatar_cover Alex Xiang
2025-01-21 229阅读

修改base.py里的WAGTAILMARKDOWN配置,因为默认allowed_tags是包含大部分HTML标签的,但是allowed_attributes基本为空,为了要支持img的尺寸修改,必须在allowed_attributes里加上width和height的属性支持。

WAGTAILMARKDOWN = {
    "autodownload_fontawesome": True,
    "allowed_tags": ['img'],  # optional. a list of HTML tags. e.g. ['div', 'p', 'a']
    #"allowed_styles": [],  # optional. a list of styles
    "allowed_attributes": {'img':['width', 'height']},  # optional. a dict with HTML tag as key and a list of attributes as value
    "allowed_settings_mode": "extend",  # optional. Possible values: "extend" or "override". Defaults to "extend".
    "extensions": ["toc"],  # optional. a list of python-markdown supported extensions
    "extension_configs": {},  # optional. a dictionary with the extension name as key, and its configuration as value
    "extensions_settings_mode": "extend",  # optional. Possible values: "extend" or "override". Defaults to "extend".
    "tab_length": 4,  # optional. Sets the length of tabs used by python-markdown to render the output. This is the number of spaces used to replace with a tab character. Defaults to 4.
}
Filter blog posts by tag wagtail
rust (4)   wagtail (4)   zola (4)   kaggle (3)   vscode (3)   deepseek (2)   gemma (2)   go (2)   winui (2)   wsl (2)