通知栏问题
来源:10-11 WebSocket触发Ajax请求-自动更新点赞数和评论数
鲨鱼辣椒辣
2021-04-07
我的点赞,评论都正常,也都能实时刷新,有了新消息之后通知栏也能变红,但是通知栏内点开没内容,提示没有未读通知!也没有报错!
8回答
-
点击铃铛只有会调用url中的这个
path('latest-notifications/', views.get_latest_notifications, name='latest_notifications'),
视图是这样
@login_required
def get_latest_notifications(request):
"""最近的未读通知"""
notifications = request.user.notifications.get_most_recent()
return render(request, 'notifications/most_recent.html',
{'notifications': notifications})022021-04-09 -
Jack
2021-04-09
是铃铛点击之后没有下面的弹框,对吗?
排错的话思路是这样:小铃铛点击,是要向这个url:
/notifications/latest-notifications/
发送get请求的,可以看http://zanhu.liaogx.com/ 这个部署的demo
然后,要么是js出错了,点击没有发送get请求,看浏览器console有什么报错;要么是后段出错了,没有正确返回数据,在开发环境中(debug=True)看后端有什么报错,在上面url函数的地方打断点调试
00 -
鲨鱼辣椒辣
提问者
2021-04-08
$(function () { const emptyMessage = '没有未读通知' const notice = $('#notifications') function CheckNotifications() { $.ajax({ url: '/notifications/latest-notifications/', cache: false, success: function (data) { if (!data.includes(emptyMessage)) { notice.addClass('btn-danger') } } }) } CheckNotifications() function update_social_activity(id_value) { const newsToUpdate = $('[news-id=' + id_value + ']'); $.ajax({ url: '/news/update-interactions/', data: {'id_value': id_value}, type: 'POST', cache: false, success: function (data) { $(".like-count", newsToUpdate).text(data.likes); $(".comment-count", newsToUpdate).text(data.comments); }, }); } notice.click(function () { if ($('.popover').is(':visible')) { notice.popover('hide'); CheckNotifications(); } else { notice.popover('dispose') $.ajax({ url: '/notifications/latest-notifications/', cache: false, success: function (data) { notice.popover({ html: true, trigger: 'focus', container: 'body', placement: 'bottom', content: data, }); notice.popover('show'); notice.removeClass('btn-danger') } }); } return false; //不是False }); // WebSocket连接,使用wss(https)或者ws(http) const ws_scheme = window.location.protocol === 'https' ? 'wss' : 'ws'; const ws_path = ws_scheme + '://' + window.location.host + '/ws/notifications/'; const ws = new ReconnectingWebSocket(ws_path); ws.onmessage = function (event) { const data = JSON.parse(event.data); switch (data.key) { case "notification": if (currentUser !== data.actor_name) { // 消息提示的发起者不提示 notice.addClass('btn-danger'); } break; case "social_update": if (currentUser !== data.actor_name) { notice.addClass('btn-danger'); } update_social_activity(data.id_value); break; case "additional_news": if (currentUser !== data.actor_name) { $('.stream-update').show(); } break; default: console.log('error', data); break } } });
012021-04-09 -
Jack
2021-04-08
用代码格式编辑一下吧,这样我好对比
012021-04-08 -
鲨鱼辣椒辣
提问者
2021-04-08
notification_list.html {% extends "base.html" %} {% load static thumbnail %} {% block title %}通知 - {{ block.super }}{% endblock %} {% block css %} <link href="{% static 'css/notifications.css' %}" rel="stylesheet"> {% endblock css %} {% block content %} <h4> {{ request.user.get_profile_name }}的未读通知列表 <a class="btn btn-dark pull-right" href="{% url 'notifications:mark_all_read' %}">全部标为已读</a> </h4> <ul class="notifications"> {% for notification in notification_list %} <li class="notification"> <div class="media"> <div class="media-object"> {% thumbnail notification.actor.picture "x75" as im %} <img src="{{ im.url }}" style="border-radius: 50%;" alt="用户头像" id="pic"> {% empty %} <img src="{% static 'img/user.png' %}" height="75px" alt="没有头像"/> {% endthumbnail %} </div> <div class="media-body"> <a class="btn btn-success btn-sm pull-right" title="标为已读" href="{% url 'notifications:mark_as_read' notification.slug %}"> <i class="fa fa-check-circle"></i></a> <strong class="notification-title"> <a href="{% url 'users:detail' notification.actor.username %}">{{ notification.actor.get_profile_name }}</a> </strong> <p class="notification-desc"> {{ notification.get_verb_display }} {% if notification.action_object %} {{ notification.action_object }} {% endif %} </p> <div class="notification-meta"> <small class="timestamp">{{ notification.created_at|timesince }}之前</small> </div> </div> </div> </li> {% empty %} 您没有收到任何通知 {% endfor %} </ul> {% endblock content %}
012021-04-09 -
鲨鱼辣椒辣
提问者
2021-04-08
{% extends "base.html" %} {% load static thumbnail %} {% block title %}通知 - {{ block.super }}{% endblock %} {% block css %} <link href="{% static 'css/notifications.css' %}" rel="stylesheet"> {% endblock css %} {% block content %} <h4> {{ request.user.get_profile_name }}的未读通知列表 <a class="btn btn-dark pull-right" href="{% url 'notifications:mark_all_read' %}">全部标为已读</a> </h4> <ul class="notifications"> {% for notification in notification_list %} <li class="notification"> <div class="media"> <div class="media-object"> {% thumbnail notification.actor.picture "x75" as im %} <img src="{{ im.url }}" style="border-radius: 50%;" alt="用户头像" id="pic"> {% empty %} <img src="{% static 'img/user.png' %}" height="75px" alt="没有头像"/> {% endthumbnail %} </div> <div class="media-body"> <a class="btn btn-success btn-sm pull-right" title="标为已读" href="{% url 'notifications:mark_as_read' notification.slug %}"> <i class="fa fa-check-circle"></i></a> <strong class="notification-title"> <a href="{% url 'users:detail' notification.actor.username %}">{{ notification.actor.get_profile_name }}</a> </strong> <p class="notification-desc"> {{ notification.get_verb_display }} {% if notification.action_object %} {{ notification.action_object }} {% endif %} </p> <div class="notification-meta"> <small class="timestamp">{{ notification.created_at|timesince }}之前</small> </div> </div> </div> </li> {% empty %} 您没有收到任何通知 {% endfor %} </ul> {% endblock content %}
00 -
鲨鱼辣椒辣
提问者
2021-04-08
老师,编辑好了
00 -
Jack
2021-04-07
您好,看对应的js和html 文件是不是写错了。求截图一下
00
相似问题