试读教程
settings.py
DRAGON_URL = 'http://localhost:9999/'
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'notifications.context_processors.dragon_url',
)
SwampDragon设置SWAMP_DRAGON_CONNECTION = ('swampdragon.connections.sockjs_connection.DjangoSubscriberConnection', '/data')
context_processors.py
from django.conf import settings
def dragon_url(request):
return {'DRAGON_URL': settings.DRAGON_URL}
home。
{% load staticfiles %}
{% load swampdragon_tags %}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Notifications demo</h1>
<!-- This is our list of notifications -->
<ul id="notifications">
{% for notification in object_list %}
<li>{{ notification.message }}</li>
{% endfor %}
</ul>
<!-- SwampDragon -->
{% swampdragon_settings %}
<script type="text/javascript" src="{% static 'swampdragon/js/dist/swampdragon.js' %}"></script>
<script type="text/javascript" src="{% static 'swampdragon/js/vendor/sockjs-0.3.4.min.js' %}"></script>
<script type="text/javascript" src="{% static 'swampdragon/js/legacy/swampdragon-vanilla.js' %}"></script>
<script type="text/javascript" src="{% static 'notifications.js' %}"></script>
<script type="text/javascript" src="{% static 'swampdragon/js/dist/datamapper.js' %}"></script>
<script type="text/javascript" src="{{ DRAGON_URL }}settings.js"></script>
<!-- notifications -->
<script type="text/javascript" src="{% static 'notifications.js' %}"></script>
</body>
</html>
控制台错误
ReferenceError: SwampDragon is not defined
var sdInstance = new SwampDragon(options);
还试图显示控制台
中的设置<script>
console.log(window.swampdragon_settings);
</script>
我endpoint "/data"
不知道怎么了。如果需要更多的信息,请告诉我。
如果需要冻结pip
backports.ssl-match-hostname==3.4.0.2
certifi==2015.4.28
Django==1.7
django-filter==0.10.0
djangorestframework==3.1.3
Markdown==2.6.2
python-dateutil==2.4.2
redis==2.10.3
six==1.9.0
sockjs-tornado==1.0.1
SwampDragon==0.4.2.2
tornado==4.2
tornado-redis==2.4.18
wheel==0.24.0
似乎问题是我所遵循的教程有一些问题。请参阅问题链接到教程。
我把notifications.js改成:
// Ask the browser for permission to show notifications
// Taken from https://developer.mozilla.org/en-US/docs/Web /API/Notification/Using_Web_Notifications
window.addEventListener('load', function () {
Notification.requestPermission(function (status) {
// This allows to use Notification.permission with Chrome/Safari
if (Notification.permission !== status) {
Notification.permission = status;
}
});
});
// Create an instance of vanilla dragon
//var dragon = swampdragon.open({onopen: onOpen, onchannelmessage: onChannelMessage});
// This is the list of notifications
var notificationsList = document.getElementById("notifications");
// New channel message received
swampdragon.onChannelMessage(function(channels, message){
// Add the notification
addNotification((message.data));
});
// SwampDragon connection open
swampdragon.open(function() {
// Once the connection is open, subscribe to notifications
swampdragon.subscribe('notifications', 'notifications');
});
// Add new notifications
function addNotification(notification) {
// If we have permission to show browser notifications
// we can show the notifiaction
if (window.Notification && Notification.permission === "granted") {
new Notification(notification.message);
}
// Add the new notification
var li = document.createElement("li");
notificationsList.insertBefore(li, notificationsList.firstChild);
li.innerHTML = notification.message;
// Remove excess notifications
while (notificationsList.getElementsByTagName("li").length > 5) {
notificationsList.getElementsByTagName("li")[5].remove();
}
}
不使用
var dragon = new VanillaDragon(...)
我改用
swampdragon.<function name>