

django自定义标签之inclusion_tag的用法
source link: https://www.qikqiak.com/post/django-custom-tag-inclusion_tag/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

django自定义标签之inclusion_tag的用法-阳明的博客|Kubernetes|Istio|Prometheus|Python|Golang|云原生
django
提供了强大的自定义标签、自定义过滤器等强大功能,今天首先介绍一下自定义标签的inclusion_tags
的用法。
1.在model目录下面新建templatetags文件夹,在该文件夹下面新建__init__.py
文件,标识该目录为一个可以引用的包
2.新建buttons.py
文件,文件内容如下:
from django import template
register = template.Library()
@register.inclusion_tag('tags/digg_button.html', takes_context=True)
def digg_button(context, comment):
# context可以获取访问的request等变量,需要将上面的takes_context设置为True,
# 而且方法的第一个参数必须为'context'
user = context['user']
if user.is_authenticated():
return {'is_digg': comment.is_digg(user), 'uuid': comment.uuid,
'up_times': comment.up_times}
return {'is_digg': False, 'uuid': comment.uuid,
'up_times': comment.up_times}
3.在模板目录新建tags目录,在目录下面新建digg_button.html
,内容如下(注意上一步中digg_button方法的返回结果):
{% if is_digg %}
<a href="javascript:void(0);" style="color:#ae3910;">
<i class="glyphicon glyphicon-thumbs-up"></i>
<span class="heart-name">赞 {{ up_times }}</span>
</a>
{% else %}
<a href="javascript:void(0);" onclick="doThumbsUp('{{ uuid }}')">
<i class="glyphicon glyphicon-thumbs-up"></i>
<span class="heart-name">赞 {{ up_times }}</span>
</a>
{% endif %}
4.然后自定义的标签digg_button
其实就已经定义好了,只是在使用的时候需要注意,我用的django 1.6.2版本,需要在使用的模板中加载buttons文件,代码如下(注意这里加载的buttons为第一步中templatetags
目录下面新建的文件):
{% load buttons %}
5.最后就可以在上一步操作的模板中使用digg_button
标签了:
{% digg_button comment %}
注意django 1.6.2版本一定需要加载标签文件,其他版本的暂未测试。
微信公众号
扫描下面的二维码关注我们的微信公众帐号,在微信公众帐号中回复◉加群◉即可加入到我们的 kubernetes 讨论群里面共同学习。

「真诚赞赏,手留余香」
Related Issues not found
Please contact @cnych to initialize the comment
✕ Error. Try later. You have blocked Push Notifications. Follow these instructions to enable Push Notifications. Subscribe to receive push notifications on latest updates You are unsubscribed to Push Notifications You are subscribed to Push Notifications SUBSCRIBE SUBSCRIBE UNSUBSCRIBE ⚡ by Webpushr
阳明的博客
是否想要接收本站的内容更新呢?
YES
NOT YET
Cookie & Data Storage Detail
Cookies & Server Data Storage Disclosure
Below are all cookies & data points that will be stored to help us enable web push notifications.
Go Back
Cookies (3)
A Cookie is a small piece of data that we will store in your browser.
Show cookie info
_webpushrPromptAction
This cookie is stored if you do not accept to receive push notifications from us and dismiss this prompt. This helps us remember your decision.
_webpushrEndPoint This cookie stores a unique URL associated with your push subscription. It is only stored if you accept to receive push notifications from us. This helps us successfully manage and send push notifications to you.
_webpushrLastVisit This cookie stores timestamp associated with your last visit on our site. It is only stored if you accept to receive push notifications from us. This helps us successfully manage and send push notifications to you. Server Data Storage (11) Server Data Storage are small data points that will be securely stored by us only if you accept to receive push notifications. Show data info date Timestamp associated with your push subscription start date
endpoint A custom URL and associated encrypted keys pointing to a push server that enable us to send you push notifications.
timezone Your local timezone relative to GMT
device_type Your device type. Possible values are Mobile or Desktop.
operating_system Your operating system such as Mac OS, Windows, Android, etc.
browser Your browser detail, such as Chrome, Firefox, Opera, Edge, etc.
ip_address Your IP address city Geographic Location: City
state Geographic Location: State
country Geographic Location: Country
session_date Date corresponding to your last visit on our site
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK