django应用程序扩展了django的消息框架,添加了粘性消息和持久性消息

django-messages-extends的Python项目详细描述


django消息扩展
==============




[![构建状态](https://travis-ci.org/mpasternak/django-messages-extends.svg?branch=master)(http s://travis ci.org/mpasternak/django messages extends)



django应用程序,用于扩展django的[消息框架](http://docs.djangoproject
.com/en/dev/ref/contrib/messages/)(`django.contrib.messages`)。框架通过添加"sticky"和
"persistent"后端消息存储。这也支持向机器到用户流程中的其他用户发送持久消息的概念。对于在机器到用户流程中向计算机到用户流程中向其他用户发送持久消息的消息,用户必须单击"关闭"按钮才能在该会话中删除

















对于在该会话中向其他用户发送"存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储存储在中间dleware或仅用于当前请求,不需要保存它。
*这与默认值非常相似,只是您必须显式关闭对话框才能删除消息。
*此后端从不保存任何内容,只模拟此操作。



sage在多个会话中被保留,直到用户单击close按钮。消息存储在默认的存储容器中
(默认为数据库)。

*只有经过身份验证的用户,消息才存储在数据库中。
*消息必须是显式读取的,并且在不关闭它的情况下会显示


以及django.

1.[下载并解压缩应用程序](https://github.com/alilozano/django messages extends)、
或使用"pip"安装:

$pip install django messages extends

2。确保"消息扩展"位于"pythonpath"上。
3.将"消息扩展"添加到"已安装的应用程序"设置中。

``python
installed\u apps=(

"消息扩展"

```

4。确保django的"MessageMiddleware"在"Middleware戥u类"设置中(默认情况下为
):

``python
Middleware戥u类=(

'django.contrib.messages.middleware.MessageMiddleware',
``

5。添加消息"将URL扩展到URL配置中。例如,为了使"http://domain.com/messages/"下的消息
可用,请将以下行添加到"urls.py"。

``python
url patterns=patterns('',
(r'^messages/',include('messages恚extends.urls'),


```

6。在您的设置中,将消息[存储后端](http://docs.djangoproject.com/en/dev/ref/contrib/messages/消息存储后端)设置为"消息扩展.storages.fallbackstorage":

`` python
``消息存储='消息扩展.storages.fallbackstorage'
```

7。使用


8设置数据库表。如果要使用捆绑模板,请将"templates"目录添加到
`template\u dirs`模板设置:



``python
`template\u dirs=(


//'path/to/messages\u extends/templates')

``



































django的消息框架提供了许多[消息级别](http://docs.djangoproject.com/en/dev/ref/contrib/message s/消息级别)用于各种目的,如成功消息、警告等。此应用程序提供了具有相同名称的常量,区别在于具有这些级别的消息将是持久的:

`` python
从消息扩展导入常量作为常量消息

NT消息级别
常量消息.debug_persistent=9
常量消息.info_persistent=19
常量消息.success_persistent=24
常量消息.warning_persistent=29
常量消息.error_persistent=39

粘性消息级别
常量消息s.debug掼u sticky=8
常量掼u消息。info掼u sticky=18
常量掼u消息。success掼u sticky=23
常量掼u消息。warning掼u sticky=28
常量掼u消息。error掼u sticky=38
``````

对于django的[消息框架](http://docs.djangoproject.com/en/dev/ref/contrib/message s/)来说,
仍然可以使用常规djangoproject api添加消息:

``python
from django.contrib import messages
消息。添加消息(request,messages.info,'hello world.')
```


或使用消息扩展中带有常量的持久消息。来自django的常量

``python
contrib import messages
来自消息的常量将导入常量扩展为常量消息
(request,constants_messages.warning_persistent,'您将看到此消息,直到您将其标记为已读。')
````


对于已登录的用户,您可能需要使用'request.user.is_authenticated()'确保
当前用户不是匿名的。为匿名用户添加
持久消息会引发"notimplementederror"。

_sticky,'您将仅在此请求中看到此消息')
``


用户sally下次登录时将看到此消息:


``python
from django.contrib import messages
from messages_extends import constants as constants嫒messages
from django.contrib.auth.models import user
sally=user.objects.get(username='sally')
messages.add嫒message(request,constants嫒message s.info嫒persistent,"hola abc desde%s"%request.user,user=sally)
````


>对于persistent storages,还有其他类似expires的参数,即datetime。


手动](http://docs.djangoproject.com/en/dev/ref/contrib/messages/显示消息)。
但是,您可能需要包含用于关闭每条消息的链接标记(即将其标记为已读)。在您的模板中,使用如下内容:

``htmldjango
{%for messages%}
<;div class="alert{%if message.tags%}alert-{{{message.tags}{%endif%}">;
{close-href用于引导程序关闭其他div
<;aclass="close"data dismission="alert"{%if message.pk%}close href="{%url message_mark_read message.pk%}"{%endif%}>;×<;/a>;
{{message}
</div>;
{%endfor%}
```

您也可以使用捆绑的模板。下面一行替换上面的代码。它允许用户使用[bootstrap styleing]删除消息(http://twitter.github.com/bootstrap/)
(您需要使用bootstrap.css和bootstrap.js)

``htmldjango
{%include"消息扩展/包含/警报"bootstrap.html"%}
```

u可以添加以下与jquery一起工作的代码:

`` javascript
$("a.close[close-ref]").click(function(e){
e.preventdefault();
$.post($(this).attr("close-ref"),"",function(){
};
}
`````


>或使用:

``htmldjango
<;script src="{%static"close alerts.js"%}">;
```


启用后,您必须通过js添加csrf代码,[请参阅django文档](https://docs.djangoproject.com/en/dev/ref/csrf/ajax)


如果不希望看到粘性警报中的"关闭"按钮,则可以使用css隐藏它们:

``css
。alert.sticky.close{
display:none;
}
````



默认情况下,您可以使用其他后端:

`` python
messages_storages=("messages_extends.storages.stickystorage"、
"messages挈extends.storages.cookie.cookie storage"、
"django.contrib.messages.storage.cookie.cookiestorage"、
"django.contrib.messages.storage.session.sessionstorage')
```


,但您可以在设置中添加或删除其他后端,以便执行该设置,
记住session storagge保存所有消息,然后必须将其放在最后。

此模块仅适用于来自应用程序的消息、用户之间的消息,您可以使用[postman](https://bitbucket.org/psam/django-postman)u其他框架以及
活动流的消息,您可以使用[django活动流](https://github.com/justquick/django活动流)


额外的功劳是:
*[steven klass](sklass@pointcircle.com)




灵感来源于[django persistent messages](https://github.com/samluescher/django persistent messages)



欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java无法在spring boot应用程序中启用本机内存跟踪   jakarta ee在Java Web项目上的多窗口   日期将Java时间戳转换为MySQL时间戳,反之亦然   java如何实现异步任务连接到服务器并解析JSON   java为什么我得到索引越界异常?   我们如何在java中以大写字母和小写字母存储同名文件   jni/java:有效不可变本机对象的线程安全发布/共享   Java将文本写入远程文件   int最小硬币算法   java如何设置/获取我在类Vehicle的主方法中创建的类Car的“ford”实例的名称?   java使用计时器在队列已满时重新调度使用者   java从字符串的末尾提取一个子字符串,直到遇到第一个空格为止?   java在SimpleApplication之外正确初始化物理状态