一个简单的应用程序,允许应用程序注册可以通过模板标签显示在管理员中的通知。
django-admin-notifications的Python项目详细描述
一个简单的应用程序,允许应用程序注册可通过模板标记显示在管理员中的通知
安装
将“管理员通知”添加到已安装的应用程序
基本用法
自定义要显示通知的管理模板。
在模板开始处加载模板标记:
{% load notification_tag %}
将标记添加到模板中。我定制了管理索引模板,并将标记放在“content main”div前面,如下所示:
{% block content %} <!-- here's the tag you add --> {% error_notifications %} <div id=''content-main">
在url.py中:
import admin_notifications admin_notifications.autodiscover()
在每个应用程序中查找一个名为“NoTimeCtudio.Py”的文件,并在存在时注册它。
通知文件可以包含任意多个函数。每一个都应该返回一个包含html的字符串,并且每个字符串都需要使用admin_notifications.register进行注册
如果返回空字符串,则不显示通知。
每次呈现管理模板时都会调用通知函数,因此避免在通知中进行繁重的计算。
notifications.py示例可能如下:
import admin_notifications from models import Url def notification(): broken_links = Url.objects.filter(status=False).count() if broken_links: return "You have %s broken link%s.<br>You can view or fix them using the <a href='/admin/linkcheck/'>Link Manager</a>." % (broken_links, "s" if broken_links>1 else "") else: return '' admin_notifications.register(notification)