在你的网站上使用emojis的简单django应用程序
django-emoji的Python项目详细描述
emoji是github gemgemoji到django的端口。
此应用程序有两个主要用例:
- 它将尝试用emojis替换::之间的项,例如: dog :(不带空格)将成为dog(:dog:)的emoji。
- 它将尝试用emojis替换unicode字符,例如用胜利符号(:v:)替换“”。
版本2发布
多亏了Tim Schilling,我们现在启用了 与核心Django的逻辑相同。因为这可能是一个突破性的变化 此应用程序的用户主要版本已被碰撞。除了 filters添加了django的标准xss保护 变化。
要获得应用程序的旧行为,请按如下方式包装筛选器:
{% autoescape off %} {{ emoji|emoji_replace }} {% endautoescape %}
快速启动
从pypi安装django emoji<;https://pypi.python.org/pypi/django emoji>;
pip install django-emoji
将“emoji”添加到已安装的应用程序设置中,如下所示:
INSTALLED_APPS = ( ... 'emoji', )
如果您想获得emojis的json列表,请将emoji urlconf包含在您的项目url.py中,如下所示:
url(r'^emoji/', include('emoji.urls')),
访问http://127.0.0.1:8000/emoji/all.json获得一个json对象,其中所有emojis都是可用的
python版本
支持django 1.5、1.6、1.7、1.8和1.9,并支持各自版本的python 2/3。
设置
这些设置可以在settings.py:
中配置Settings name | Description |
---|---|
^{tt3}$ | The template string that is used for creating the <img> tag when converting an emoji to an image. Default: ^{tt4}$ |
^{tt5}$ | Whether to put the unicode character that corresponds to an emoji as the alt text in ^{tt6}$. Default: ^{tt7}$ |
^{tt8}$ | Whether to automatically convert HTML encoded unicode characters into emojis. Default: ^{tt7}$ |
使用量
API
Python
python类Emoji是一个单例,将返回相同的 实例之间的实例。加载时emoji将加载 所有的emojis和它们的unicode等价物都存储在内存中。
Call | Description |
---|---|
^{tt11}$ | A list of all known emojis |
^{tt12}$ | Replaces all emojis between ^{tt13}$ |
^{tt14}$ | The name for a given unicode character |
^{tt15}$ | Replaces all known unicode emojis |
^{tt16}$ | Replaces all HTML encoded unicode characters |
javascript
浏览器版本将所有emojis缓存在localStorageso中 不会有那么多往返服务器只是为了得到一个列表 关于情感的。
note:依赖于jquery或其他导出$.get的库。
Call | Description |
---|---|
^{tt19}$ | Where to fetch the list of all available emojis |
^{tt20}$ | Load all emojis from the server |
^{tt21}$ | Get the URL to an emoji of a name or return the names of all known emojis |
^{tt22}$ | Replace all ^{tt13}$ style emojis with images |
^{tt24}$ | Empty the browser cache |
示例
使用python模板替换emoji,方法是在模板中加载标记:
{% load emoji_tags %} {{ blog_post.body|emoji_replace }} {{ blog_post.body|emoji_replace_unicode }} {{ blog_post.body|emoji_replace_html_entities }}
使用javascript替换emojis(例如,在用户保存正在编写的内容之前显示预览):
{% load emoji_tags %} <script src="{% static 'emoji/js/emoji.js' %}"></script> {% emoji_load %} Emoji.get('dog') // => url stub to dog emoji or falsy Emoji.get() // => all emojis available Emoji.replace("It's raining :cats: and :dogs:.") // => It's raining <img src="..." alt="cats" class="emoji"> and <img src="..." alt="dogs" class="emoji">
emoji_load所做的是设置端点以检索所有可用emoji的列表,因此仅当包含emoji url时才起作用。
这相当于做:
Emoji.setDataUrl('{% url 'emoji:list.json' %}').load();
也可用作模板存根:
{% include 'emoji/script.html' %}
从python内部使用,其中emoji类模拟dict的某些行为:
from emoji import Emoji Emoji['dog'] # => url stub to dog emoji or None 'dog' in Emoji # => True Emoji.keys() # => a list of all emojis by name Emoji.replace("It's raining :cats: and :dogs:") # => It's raining <img src="..." alt="cats" class="emoji"> and <img src="..." alt="dogs" class="emoji">
替换Unicode表情符号
emoji能够从unicode中为您提供emoji的名称 性格。它还可以替换 字符串及其图像替换。
用法:
>>> from emoji import Emoji >>> Emoji.name_for(u'\U000148b') kiss >>> Emoji.replace_unicode(u'I send a \U0001f48b!') I send a <img src="/static/emoji/img/kiss.png" alt="kiss" class="emoji">
注释:
为了更好地使用unicode替换,请使用python构建 是建立在广泛的Unicode字符支持。从1.2版开始 是为处理Unicode代理项对而添加的代码,它应该 在狭窄的建筑上工作得很好。但这还没有经过生产测试 好好试试。请报告发现的任何错误。
要测试python的构建是窄的还是宽的,请运行 接下来,如果您遇到异常,则表示您正在运行窄版本。
>>> print(unichr(0x0001f48b)) ValueError: unichr() arg not in range(0x10000) (narrow Python build)