Django - Tutorial 029. Adding private messages and chats on the site - Part 1

чат, message, Django, chat, сообщение

According to the tradition, I will tell you about my experiences on the introduction of new functionality on the site. At the moment, this functionality is a personal message between users. Of course, this now does not work as well as in well-known social networks ... but in the end everything will work. The main feedback on the forum, please.

So. I really wanted to add personal messages on the site, especially since I already mentioned this six months ago. The question remained, how to do it. When searching on the Internet it was possible to stumble upon an option when the following data model is being formed.

  • Id of the message
  • from_user - sender
  • to_user - recipient
  • pub_date - the date of the message
  • message - message content

Tried to implement this option, but I was stopped by the fact that suddenly after personal messages I want to make chats? So why not immediately lay the foundation for chats?

Chat and Message models

This would be an excellent option for further development of the resource. But in this case, you need to create two models of Chat and Message .

# -*- coding: utf-8 -*-

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

class Chat(models.Model):
    DIALOG = 'D'
    CHAT = 'C'
        (DIALOG, _('Dialog')),
        (CHAT, _('Chat'))

    type = models.CharField(
    members = models.ManyToManyField(User, verbose_name=_("Member"))

    def get_absolute_url(self):
        return 'users:messages', (), {'chat_id': }

class Message(models.Model):
    chat = models.ForeignKey(Chat, verbose_name=_("Chat"))
    author = models.ForeignKey(User, verbose_name=_("User"))
    message = models.TextField(_("Message"))
    pub_date = models.DateTimeField(_('Message date'),
    is_readed = models.BooleanField(_('Readed'), default=False)

    class Meta:

    def __str__(self):
        return self.message

If the Message model should be clear. There is a text message, status, whether it was read, the author of the message and the chat in which it was sent, as well as the date the message was sent.

That's the model of Chat somewhat more difficult to be. First, chats can be the spirit of species. The first is a personal conversation between two people. The second kind is a collective chat. Why was this done? This was necessary in order to simplify the search for the desired chat, when sending a message to the user from his personal page. Unfortunately at the moment, only such a way is done to send the first message to the user. That is, go to his page and click the button to write a message. In the future, you can find the user from the dialog page on your personal page. This is still a temporary limitation within the first User Story, which I wrote myself.

Each chat has many-to-many connection, which is responsible for the list of participants within the chat. Due to this, you can limit the viewing of chats, as well as the ability to write to chats, if the user was not invited to this chat room.

What kind of views will we need and what routes can be done? To implement this functionality in the simplest form, I had to write three views and three routes to respectively.

url(r'^dialogs/$', login_required(views.DialogsView.as_view()), name='dialogs'),
url(r'^dialogs/create/(?P<user_id>\d+)/$', login_required(views.CreateDialogView.as_view()), name='create_dialog'),
url(r'^dialogs/(?P<chat_id>\d+)/$', login_required(views.MessagesView.as_view()), name='messages'),

We will not go deep into which particular application we will use these routes - this is not a matter of principle.

In fact, there are three views, one for the list of dialogues for the user, another for creating a dialog from the page of another user, and the third for the messages in the dialog.

Message form

In this case, you can use the form for the model. We indicate the model, as well as which fields should be displayed with the label removed from the input field.

By default, you will have the usual textarea. I use my own WYSIWYG editor.

class MessageForm(ModelForm):
    class Meta:
        model = Message
        fields = ['message']
        labels = {'message': ""}

Views and Templates

Display a list of dialogs

To get a list of all the dialogs in which the user is involved, you need to filter all chats by participants, that is, by the Many-toMany members.

class DialogsView(View):
    def get(self, request):
        chats = Chat.objects.filter(members__in=[])
        return render(request, 'users/dialogs.html', {'user_profile': request.user, 'chats': chats})

In this case, I have a template for dialogs, in which I send an authorized user and a chat list. An active user will be needed to correctly display the messages read and not read in the dialog list.

The most interesting is the list of dialogs and its layout, so only the part responsible for the layout will be represented from the template.

<div class="panel">
        {% load tz %}
        {% if chats.count == 0 %}
            <div class="panel panel-body">{% trans "There is not a single dialogue started" %}</div>
        {% endif %}
        {% for chat in chats %}
            {% if chat.message_set.count != 0 %}
                {% with last_message=chat.message_set.last %}
                    {% get_companion user chat as companion %}
                    <a class="list-group-item {% if companion == and not last_message.is_readed %}unreaded{% endif %}" href="{{ chat.get_absolute_url }}">
                        <img class="avatar-messages" src="{{ companion.userprofile.get_avatar }}">
                        <div class="reply-body">
                            <ul class="list-inline">
                                <li class="drop-left-padding">
                                    <strong class="list-group-item-heading">{{ companion.username }}</strong>
                                <li class="pull-right text-muted"><small>{{ last_message.pub_date|utc }}</small></li>
                            {% if companion != %}
                                    <img class="avatar-rounded-sm" src="{{ }}">
                                    <div class="attached-reply-body {% if not last_message.is_readed %}unreaded{% endif %}">{{ last_message.message|truncatechars_html:"200"|safe|striptags }}</div>
                            {% else %}
                                <div>{{ last_message.message|truncatechars_html:"200"|safe|striptags }}</div>
                            {% endif %}
                {% endwith %}
            {% endif %}
        {% endfor %}

You can see my custom inline tag in the template. This tag is responsible for returning the interlocutor in the dialog in order to build on the information about the interlocutor an adequate layout for the list of dialogues and indication of the read and unread messages in the dialog list.

def get_companion(user, chat):
    for u in chat.members.all():
        if u != user:
            return u
    return None

Thus, the list of dialogs will look like this:

Current dialog and messages

To display the current dialog and messages, you will need more complex logic. The fact is that here the access to the chat is carried out by ID, but it is necessary to make not only an attempt to get a dialogue, but also to check if there is a user in the list of participants who is trying to get into this chat room. If it does not exist in the list of participants, then access to this chat room is forbidden to him. Among other things, the same view treats the sending of messages and the marking of messages read.

class MessagesView(View):
    def get(self, request, chat_id):
            chat = Chat.objects.get(id=chat_id)
            if request.user in chat.members.all():
                chat = None
        except Chat.DoesNotExist:
            chat = None

        return render(
                'user_profile': request.user,
                'chat': chat,
                'form': MessageForm()

    def post(self, request, chat_id):
        form = MessageForm(data=request.POST)
        if form.is_valid():
            message =
            message.chat_id = chat_id
   = request.user
        return redirect(reverse('users:messages', kwargs={'chat_id': chat_id}))

Message list template

{% if not chat %}
    <div class="panel panel-body">
        {% trans "It's impossible to start a conversation.No user is found or you do not have access to this conversation." %}
{% else %}
    {% load tz %}
    {% if chat %}
        <div id="messages" class="panel">
            <div id="innerMessages">
                {% for message in chat.message_set.all %}
                        {% include 'users/message.html' with message_item=message %}
                {% endfor %}
    {% endif %}
    <div id="message_form">
        <form id="message-form" class="panel panel-body" method="post" >
            {% load bootstrap3 %}
            {% csrf_token %}
            {% bootstrap_form form %}
            <button type="submit" class="btn btn-default btn-sm" onclick="return ETextEditor.validateForm('message-form')"><span class="ico ico-comment"></span>{% trans "Отправить" %}</button>
{% endif %}

Template of the message itself

{% url 'users:profile' as the_user_url%}
{% load tz %}
<div class="list-group-item {% if not message_item.is_readed %}unreaded{% endif %}">
    <a href="{{ the_user_url }}"><img class="avatar-comment" src="{{ }}"></a>
    <div class="reply-body">
        <ul class="list-inline">
            <li class="drop-left-padding">
                <strong class="list-group-item-heading"><a href="{{ the_user_url }}">{{ }}</a></strong>
            <li class="pull-right text-muted"><small>{{ message_item.pub_date|utc }}</small></li>
        <div>{{ message_item.message|safe }}</div>

Example of the resulting dialog

Starting a conversation with the user

At the moment, only one method is implemented to start a conversation with the user. It is necessary to go to the user's page and click on the "Write a message" button, then a link will be sent through the link in which the chat will be created or an already existing chat with this user is found. This is used to check whether the chat is a dialogue or a conversation between several users. This makes it possible to simplify the search for the necessary dialog.

class CreateDialogView(View):
    def get(self, request, user_id):
        chats = Chat.objects.filter(members__in=[, user_id], type=Chat.DIALOG).annotate(c=Count('members')).filter(c=2)
        if chats.count() == 0:
            chat = Chat.objects.create()
            chat = chats.first()
        return redirect(reverse('users:messages', kwargs={'chat_id':}))

Be sure to check for the number of users, since there can be only two users in a personal conversation, well, we check that these users are our authorized users, and also the user with whom we are trying to start a conversation.

After the chat is created, we redirect to the message page.

For Django I recommend VDS-server of Timeweb hoster .

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.
Support the author Donate

Сделал всё в точности как написано, но возникает ошибка, возможно надо было что то импортировать воно это точно), мой первый проект на джанго, не кидайтесь тапками ). И так собственно ошибка:

NoReverseMatch at /messages/dialogs/

'users' is not a registered namespace
содержаниявот здесь думаю нне робит)
from django.shortcuts import render, redirect
from django.views import View
from .models import Chat
from .forms import MessageForm
from django.db.models import Count
from django.urls import reverse
from django.contrib import auth

# Create your views here.

class DialogsView(View):
    def get(self, request):
        chats = Chat.objects.filter(members__in=[])
        return render(request, 'users/dialogs.html', {'user': request.user, 'chats': chats})

дальше всё как в статье

Скорее ошибка в том, что у вас App не зарегистрирован в проекте.

from django.apps import AppConfig
class UsersConfig(AppConfig):
    name = 'users'

Откуда вы взяли приложение users?
Я создал было для данной статьи отдельное приложение 'Messages', в нём есть файл с содержанием:

from django.apps import AppConfig

class MessagesConfig(AppConfig):
    name = 'Messages'
у вас есть отдельно статья по созданию 'user.apps.UsersConfig'?

users - это приложение для отображения личного кабинета пользователей. И некоторые страницы отображаются в рамках этого приложения.
Полагаю, что в вашем случае нужно сменить users на Messages в ряде мест в шаблонах, например, эту строку

return redirect(reverse('users:messages', kwargs={'chat_id':}))
заменить на
return redirect(reverse('Messages:messages', kwargs={'chat_id':}))
Примерно так всё и поменять, отслеживая, чего не хватает.
Конкретно для личного кабинета пользователей статьи нет. Статья основана конкретно на коде данного сайта и является обобщённым примером основных моментов, что-то может быть опущено. Многие статьи ориентированы на наличие определённого опыта у программиста. Поэтому данные статьи и не являются полным руководством к повторению.

Ошибка не изменилась.

Ну а шаблоны? которые html. Там тоже есть {% url 'users:blablabla' %}


ну ошибка такая же но только не users а messeages найти не может

А вы когда создавали приложение для комментариев, вы его urls подключили в файле всего проекта?

Например, в этой статье есть два приложения: home и knowledge и их urls подключаются в файле всего проекта. Вы их подключали?

да, конечно, и в instaled_apps прописал как Messages.apps.MessagesConfig


и ещё, эта ссылка работает пока тебе или ты не напишешь сообщения
url(r'^dialogs/$', login_required(views.DialogsView.as_view()), name='dialogs'),

Внимательнее проанализируйте код из статьи и вывод ошибок, вы однозначно где-то накосячили. Вполне возможно, что вы не зарегистрировали пользовательский тег get_companion, Вы знаете как регистрировать пользовательские теги ?


Добрый день, очень интересные статьи у Вас на сайте, касательно Django  и  Python. Когда планируете создать часть 2 Чата. Евгений, это ведь не чат в режиме реального времени? каждый раз приходится обновлять страницу. Не планируете использовать для чата channels ?

Добрый день!

Спасибо за отзыв.
Всё планирую, а также планирую использовать channels, но пока не так много времени на внедрение всего функционала на сайте. Поэтому по срокам вообще ничего не могу сказать.



Thank you for the article. Is there the github folder or the source code folder for this chat features, because for the code provided here sometimes I don't really know the exact place to put and make the project work.

Hi, unfortunately - no. But You can ask your question on the forum of this site . I try answer on questions in all sections of forum.


Only authorized users can post comments.
Please, Log in or Sign up
July 20, 2019, 10:59 a.m.
Nikolaj Morozov

C++ - Test 001. The first program and data types

  • Result:73points,
  • Rating points1
July 19, 2019, 9:51 a.m.
Anastasia Dutchina

C++ - Test 001. The first program and data types

  • Result:53points,
  • Rating points-4
July 19, 2019, 9:46 a.m.
Anastasia Dutchina

Qt - Test 001. Signals and slots

  • Result:57points,
  • Rating points-2
Last comments
July 21, 2019, 6:03 a.m.
Evgenij Legotskoj

да, наверное, 32-х разрядную поддержку уже давно поа было выкинуть. К слову, у вас много проектов под Android? Часто много где вижу вопросы о том, пишет ли кто-то вообще на Qt под мобильные си...
July 20, 2019, 2:41 p.m.
Andrej Jankovich

Очень полезная информация, увы уже выкинул поддержку 32 битных бедняг.
July 20, 2019, 9:31 a.m.

Вот так qDebug()<<"position:"<<event->scenePos();
July 20, 2019, 8:49 a.m.

Добрый день. Как можно узнать координату на графической сцене при отпускании клавиши мыши?
Now discuss on the forum
July 21, 2019, 6:07 a.m.
Evgenij Legotskoj

Если вы про этот метод QRectF MoveItem::boundingRect() const{ return QRectF (-30,-30,60,60);} То мне нужно было, чтобы координата (0,0) была по центру квадрата в его локальной си...
July 20, 2019, 11:04 a.m.

Так и с ресурсами работает QImage image(":/Images/Images/1.png");
July 19, 2019, 10:55 a.m.

Да. Там похоже каждое устройство генерирует свой токен, этот токен нужно как то получать и использовать в запросе. Но как это делать я пока не понял.
July 19, 2019, 9:31 a.m.

Добрый день. Повернул ListView в горизонтальное положение. При прокрутке эллементы выстраиваются у левого края окна. Как в QMK в ListView сделать центрирование по центру? ...
July 18, 2019, 2:27 a.m.

Когда отрисовки не видно, объекты AreaSelector создаются (при нажатии и движении мышкой), но почему то не срабатывает paint() у них, хотя делаю update этой области. Причем эти области мо...
Looking for a Job?
5,000.00 руб. - 15,000.00 руб.
Moskovskiy, Moscow, Russia
25,000.00 руб. - 30,000.00 руб.
Разработчик Qt/C++
Barnaul, Altai Krai, Russia

For registered users on the site there is a minimum amount of advertising

Join us
© EVILEG 2015-2019
Recommend hosting TIMEWEB