python - Django - 无效的过滤漂白剂

标签 python django

我正在迁移到“bleach”,我遵循了 tuuroial 中的所有步骤,但是当我在 html 中调用 {{ post.content|bleach }} 时出现错误。我使用的是 Django 1.8 版本。另外,在我的管理员中,它无法识别我的 CKeditor。

页面中的错误信息是:

Request Method: GET
Exception Value:    Invalid filter: 'bleach'

在服务器日志中我得到:

/home/ubuntu/workspace/wpage/urls.py:26: RemovedInDjango19Warning: Default value of 'RedirectView.permanent' will change from True to False in Django 1.9. Set an explicit value to silence this warning.
  url(r'^.*/$', RedirectView.as_view(url='/blog/')),

这是我的一些文件:

设置.py:

INSTALLED_APPS = (
...
    'django_bleach',
)
...
BLEACH_ALLOWED_ATTRIBUTES = ['href', 'title', 'style']
BLEACH_ALLOWED_STYLES = [
    'font-family', 'font-weight', 'text-decoration', 'font-variant']
BLEACH_STRIP_TAGS = True
BLEACH_STRIP_COMMENTS = False
BLEACH_DEFAULT_WIDGET = 'ckeditor.widgets.CKEditorWidget'

模型.py:

from django.db import models
from django_bleach.models import BleachField
class Post(models.Model):
    ...
    content = BleachField()

base.html:

{% load staticfiles %}
{% load bootstrap3 %}
{% load bleach_tags %}
...

post.html:

{% extends "base.html" %}
{% block content %}
<div> {{ post.content|bleach }} </div>

最佳答案

您需要在使用 bleach 标记的任何模板中加载 bleach_tags。将其加载到父模板中是不够的。

{% extends "base.html" %}
{% load bleach_tags %}

{% block content %}
<div> {{ post.content|bleach }} </div>

RemovedInDjango19Warning 是一个完全独立的问题。正如消息所述,您可以通过设置显式值来使其静音:

RedirectView.as_view(url='/blog/', permanent=False)

您使用的正则表达式 '^.*/$' 会将每个以斜杠结尾的网址永久重定向到 /blog/。我很确定你不想这样做。我会删除该模式(或至少设置permanent=False),直到您满意它正在工作并确定您希望它是永久的。

关于python - Django - 无效的过滤漂白剂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34227033/

相关文章:

python - 在 django 中使用自定义 http header 时遇到问题

python - Django Admin 中的密码小部件和密码散列

python - Keras 在多个输出的多个损失中寻找最佳损失以进行反向传播

python - 有没有办法在不进行硬编码的情况下编写相同的 try- except 多行代码? Python

java - 如何调用Rest API并使用该API中的数据?

python - 如何在 Pandas Python 3.X 中划分两个数据帧,并将 NaN、inf 值替换为 0

python - 根据文件名将目录中的多个 .xlsx 文件读入单独的 Pandas 数据框中

python - Django中对象的含义

python - Django:如何在抽象模型类中设置ForeignKey related_name?

python - Django 查询集中的正则表达式