我有个django博客。我需要使用附图来检查博客文章中的文本是否是英文的,然后使用api来纠正文本错误。
我在django博客的虚拟环境中安装了api
pip安装加固剂
项目,并将其包含在已安装的应用程序中,但在博客base.html中,我尝试加载它,并使用它的功能来检查帖子标题是否是英文的,但我什么也没做。如何解决这个问题?以下是我的html代码:
{% load enchant %}
{% dictionary = enchant.Dict("en_US") %}
<p>{% dictionary.check(post.title) %}</p>
当我运行服务器时没有错误,但是html页面上没有任何错误。注意:根据API,它应该是段落标签中的True of False。“我在python外壳中测试了它。”
发布于 2017-06-28 10:06:46
你不能这样做。跟我来:)
有一个没有模板标记,只有才能使用django (我从文档中学到的东西)。您的代码dictionary = enchant.Dict("en_US")
在django后端上是正常的,但是它也不适合django模板。
为此,您可以创建一个定制的模板标记,以便在您的python代码和模板语言之间进行连接。
您可以这样做,它是有效的:
Tree:
templatetags
templatetags/__init__.py
templatetags/pyenchant_tags.py
templatetags/pyenchant_tags.py文件
import enchant
from django import template
register = template.Library()
@register.simple_tag
def please_enchant_my_string(language, string):
d = enchant.Dict(language)
return d.check(string)
带有标记调用的模板部件:
{% load pyenchant_tags %}
<div>
{% please_enchant_my_string 'en_US' post.title %}
</div>
https://stackoverflow.com/questions/44798267
复制相似问题