2015-12-07 122 views

回答

0

如果外部库是不是对你的负担,那么你必须尝试django-bleach,就足够你的要求。它将返回仅包含您指定的允许标记的有效HTML。

配置: settings.py中

BLEACH_ALLOWED_TAGS = ['p', 'b', 'i', 'u', 'em', 'strong', 'a'] 
BLEACH_ALLOWED_ATTRIBUTES = ['href', 'title', 'style'] 
BLEACH_STRIP_TAGS = True 

使用案例: 1.在你的模型:

from django import models 
from django_bleach.models import BleachField 

class Post(models.Model): 
    title = models.CharField() 
    content = BleachField() 

2.在您的形式:

class PostForm(forms.ModelForm): 
    content = BleachField() 
    class Meta: 
     model = Post 
     fields = ['title', 'content'] 
  • 在你的模板:

    {%负载bleach_tags%}

    {{unsafe_html |漂白}}

  • 更多的用法,我建议你必须阅读的文档。它非常简单直接。

    documentation

    +0

    这对我有用,谢谢! – Pieter

    相关问题