Синтаксис языка шаблонов Django для уведомлений навигационной панели - PullRequest
1 голос
/ 20 апреля 2019

Я пытаюсь использовать язык шаблонов Django в моем проекте Django Channels 2.1.2 для вывода непрочитанных сообщений чата во всплывающем окне уведомлений в стиле Facebook.

Список непрочитанных chatmessages (в соответствующих threads) не отображается, потому что у меня проблемы с правильным синтаксисом.

Так выглядит внешний вид. При нажатии значка сообщения уведомление исчезает.

inbox number notification popout

У меня есть Notification модель

class Notification(models.Model):
    notification_user = models.ForeignKey(User, on_delete=models.CASCADE)
    notification_chat = models.ForeignKey(ChatMessage, on_delete=models.CASCADE)
    notification_read = models.BooleanField(default=False)

    def __str__(self):
        return f'{self.id}'

navbar.html

 {% if user.is_authenticated %}
  <li id="notification_li" class="nav-item">
    <a class="nav-link" href="#" id="notificationLink">
      <i class="fas fa-envelope"></i>&nbsp; Inbox</a>
      {% for notifications in notification %}
      <span id="notification_id">{{ notifications.notification_chat }}</span>
      {% endfor %}
      <div id="notificationContainer">
            <div id="notificationTitle">Notifications</div>
            <div id="notificationsBody" class="notifications">
            {{ notification.notification_chatessage?? }}
            </div>
            <div id="notificationFooter"><a href="{% url 'chat:inbox' %}">See All</a></div>
      </div>
  </li>

base.html

  <script>
    $(document).ready(function() {
      $("#notificationLink").click(function() {
        $("#notificationContainer").fadeToggle(300);
        $("#notification_id").fadeOut("slow");
        return false;
      });

      //Document Click hiding the popup
      $(document).click(function() {
        $("#notificationContainer").hide();
      });

      //Popup on click
      $("#notificationContainer").click(function() {
        return false;
      });
    });
  </script>

context_processors.py

def notification(request):
    if request.user.is_authenticated:
        notification = Notification.objects.filter(notification_user=request.user)
        return {'notification':notification}
    return Notification.objects.none()

у меня контекстный процессор тоже добавлен в настройки в правильном месте. notification_id следует отправлять с помощью сообщения WebSocket и обновлять каждый раз при отправке нового сообщения (мне все еще не удалось это сделать успешно).

consumers.py

async def websocket_receive(self, event):
        # when a message is received from the websocket
        print("receive", event)

        message_type = event.get('type', None)  #check message type, act accordingly
        if message_type == "notification_read":
             # Update the notification read status flag in Notification model.
             notification = Notification.object.get(id=notification_id)
             notification.notification_read = True
             notification.save()  #commit to DB
             print("notification read")

        front_text = event.get('text', None)
        if front_text is not None:
            loaded_dict_data = json.loads(front_text)
            msg =  loaded_dict_data.get('message')
            user = self.scope['user']
            username = 'default'
            if user.is_authenticated:
                username = user.username
            myResponse = {
                'message': msg,
                'username': username,
                'notification': notification_id  # send a unique identifier for the notification
            }
            ...

thread.html

   ...
      // below is the message I am receiving
      socket.onmessage = function(e) {
        var data = JSON.parse(event.data);
        // Find the notification icon/button/whatever
        // and show a red dot, add the notification_id to element as id or data attribute.
        console.log("message", e)
        var chatDataMsg = JSON.parse(e.data)
        chatHolder.append('<li>' + chatDataMsg.message + ' from ' + chatDataMsg.username + '</li>')
      }

Помимо того, что помог бы мне с этим вопросом, я был бы очень признателен за любые хорошие учебные ресурсы.

1 Ответ

1 голос
/ 20 апреля 2019

Для обращения к уведомлению вы должны использовать {{notifications.notification_chat.message}}. Кроме того, для отображения всех уведомлений вам придется перебрать все уведомления.

navbar.html

{% if user.is_authenticated %}
              <li id="notification_li" class="nav-item">
                <a class="nav-link" href="#" id="notificationLink">
                  <i class="fas fa-envelope"></i>&nbsp; Inbox</a>
                  {% for notifications in notification %}

                  <span id="inbox-{{notifications.id}}">{{ notifications.notification_chat.message }}</span>

                  {% endfor %}
                  <div id="notificationContainer">
                        <div id="notificationTitle">Notifications</div>
                        <div id="notificationsBody" class="notifications">
                        {% for notifications in notification %}

                            <span id="notification-{{notifications.id}}">{{ notifications.notification_chat.message }}</span>

                        {% endfor %}
                        </div>
                        <div id="notificationFooter"><a href="{% url 'chat:inbox' %}">See All</a></div>
                  </div>
              </li>

Я также заметил, что в вашем thread.html вы не обновляете уведомления, когда получаете ответ от сервера. Вы можете использовать идентификаторы для prepend новых уведомлений.

...