跳转到主要内容

为django模型、表单和模板提供一组HTML清理工具。

项目描述

Django HTML Sanitizer提供了一套在django中轻松清理/转义/清理HTML输入的实用工具。该应用程序基于bleach,一个出色的Python HTML清理器。

依赖项

安装

首先需要安装此包(或从pypi手动下载)

pip install django-html_sanitizer

然后,将 sanitizer 添加到django的 settings.py 中的 INSTALLED_APPS

INSTALLED_APPS = (
    # other apps
    "sanitizer",
)

模型使用

与bleach类似,django sanitizer是一个白名单(仅允许指定的标签和属性)HTML清理器。Django sanitizer提供两个模型字段,可以自动清理文本值; SanitizedCharFieldSanitizedTextField

这些字段接受额外的参数

  • allowed_tags: 允许的HTML标签列表

  • allowed_attributes: 允许的HTML属性列表,或每个键的属性列表的字典

  • allowed_styles: 如果“style”是允许的属性之一,则允许的样式列表

  • strip: 一个布尔值,指示是否转义或删除违规的标签/属性

以下是如何在django模型中使用它

from django.db import models
from sanitizer.models import SanitizedCharField, SanitizedTextField

class MyModel(models.Model):
    # Allow only <a>, <p>, <img> tags and "href" and "src" attributes
    foo = SanitizedCharField(max_length=255, allowed_tags=['a', 'p', 'img'],
                             allowed_attributes=['href', 'src'], strip=False)
    bar = SanitizedTextField(max_length=255, allowed_tags=['a', 'p', 'img'],
                             allowed_attributes=['href', 'src'], strip=False)
    foo2 = SanitizedCharField(max_length=255, allowed_tags=['a', 'p', 'img'],
                             allowed_attributes={'img':['src', 'style']},
                             allowed_styles=['width', 'height'], strip=False)

表单使用

在django表单中使用django HTML sanitizer与模型使用非常相似

from django import forms
from sanitizer.forms import SanitizedCharField

class MyForm(forms.Form):
    # Allow only <a>, <p>, <img> tags and "href" and "src" attributes
    foo = SanitizedCharField(max_length=255, allowed_tags=['a', 'p', 'img'],
                             allowed_attributes=['href', 'src'], strip=False)
    bar = SanitizedCharField(max_length=255, allowed_tags=['a', 'p', 'img'],
                             allowed_attributes=['href', 'src'], strip=False, widget=forms.Textarea)
    foo2 = SanitizedCharField(max_length=255, allowed_tags=['a', 'p', 'img'],
                             allowed_attributes={'img':['src', 'style']},
                             allowed_styles=['width', 'height'], strip=False)

模板使用

Django 清理器在模板中提供几种清理 HTML 的方式。

escape_html 模板标签

示例用法

{% load sanitizer %}
{% escape_html post.content "a, p, img" "href, src, style" "width"%}

假设 post.content 包含字符串 ‘<a href =”#” style=”width:200px; height=”400px”>Example</a><script>alert(“x”)</script>’,上面的标签将输出

'<a href ="#" style="width:200px;">Example</a>&lt;script&gt;alert("x")&lt;/script&gt;'

在 django 1.4 中,您也可以使用关键字参数

{% escape_html '<a href="">bar</a>' allowed_tags="a,img" allowed_attributes="href,src" allowed_styles="width" %}

strip_html 模板标签

示例用法

{% load sanitizer %}
{% strip_html post.content "a, p, img" "href, src" %}

如果 post.content 包含字符串 ‘<a href =”#”>Example</a><script>alert(“x”)</script>’,这将给出

'<a href ="#">Example</a>alert("x")'

escape_html 过滤器

根据设置转义字符串中的 HTML 标签。要使用此过滤器,您需要在 settings.py 中放置以下变量

  • SANITIZER_ALLOWED_TAGS - 允许的标签列表(默认为空列表)

  • SANITIZER_ALLOWED_ATTRIBUTES - 允许的属性列表(默认为空列表)

  • SANITIZER_ALLOWED_STYLES - 如果设置了样式属性,则允许的样式列表(默认为空列表)

例如,如果我们在 settings.py 中有 SANITIZER_ALLOWED_TAGS = ['a']SANITIZER_ALLOWED_ATTRIBUTES = ['href']SANITIZER_ALLOWED_STYLES = ['width'],那么执行

{% load sanitizer %}
{{ post.content|escape_html }}

如果 post.content 包含字符串 ‘<a href =”#” style=”width:200px; height:400px”>Example</a><script>alert(“x”)</script>’,它将给出

'<a href ="#" style="width=200px;">Example</a>&lt;script&gt;alert("x")&lt;/script&gt;'

strip_html 过滤器

类似于 escape_html 过滤器,但它移除受影响的 HTML 标签。

例如,如果我们在 settings.py 中有 SANITIZER_ALLOWED_TAGS = ['a']SANITIZER_ALLOWED_ATTRIBUTES = ['href'],那么执行

{% load sanitizer %}
{{ post.content|strip_html }}

如果 post.content 包含字符串 ‘<a href =”#”>Example</a><script>alert(“x”)</script>’,我们将得到

'<a href ="#">Example</a>alert("x")'

更改日志

版本 0.1.5

  • 对 smart_unicode 和 basestring 的修复(支持 python 3.x)

版本 0.1.4

  • CharFieldTextFieldstrip_htmlescape_html 现在支持 allowed_styles(感谢 cltrudeau),

  • 添加了使用关键字参数的模板标签使用示例,因为 Django 1.4 已经发布

版本 0.1.2

  • allowed_tagsallowed_attributes 在 CharField 和 TextField 中的默认值现在是 []

项目详情


下载文件

下载适用于您平台的项目文件。如果您不确定选择哪个,请了解有关 安装软件包 的更多信息。

源分布

django-html_sanitizer-0.1.5.tar.gz (6.0 kB 查看散列)

上传时间

构建分布

django_html_sanitizer-0.1.5-py2.py3-none-any.whl (10.6 kB 查看散列)

上传时间 Python 2 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面