The Flask Mega-Tutorial Part XXI: User Notifications

This is the twenty first installment of the Flask Mega-Tutorial series, in which I'm going to add a private message feature, along with user notifications that appear in the navigation bar without the need to refresh the page.

For your reference, below is a list of the articles in this series.

In this chapter I want to continue working on improving the user experience of my Microblog application. One aspect that applies to a lot of applications is the presentation of alerts or notifications to the user. Social applications show these notifications to let you know you've got new mentions or private messages, usually by showing a little badge with a number in the top navigation bar. While this is the most obvious usage, the notification pattern can be applied to a lot of other types of applications to inform the user that something requires their attention.

But to show you the techniques involved in building user notifications, I needed to extend Microblog with a feature that can benefit from them, so in the first part of this chapter I'm going to build a user messaging system that allows any user to send a private message to another user. This is actually simpler than it sounds, and it will be a good refresher on core Flask practices and a reminder of how lean, efficient and fun programming with Flask can be. And once the messaging system is in place, I'm going to discuss some options to implement a notification badge that shows a count of unread messages.

The GitHub links for this chapter are: Browse, Zip, Diff.

Private Messages

The private messaging feature that I'm going to implement is going to be very simple. When you visit the profile page of a user, there will be a link to send that user a private message. The link will take you to a new page in which a web form takes the message. To read messages sent to you, the navigation bar at the top of the page will have a new "Messages" link, that will take you to a page that is similar in structure to the index or explore pages, but instead of showing blog posts it will show messages other users sent you.

The following sections describe the steps I took to implement this feature.

Database Support for Private Messages

The first task is to extend the database to support private messages. Here is a new Message model:

app/models.py: Message model.

class Message(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    sender_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    recipient_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    body = db.Column(db.String(140))
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)

    def __repr__(self):
        return '<Message {}>'.format(self.body)

This model class is similar to the Post model, with the only difference that there are two user foreign keys, one for the sender and one for the recipient. The User model can get relationships for these two users, plus a new field that indicates what was the last time users read their private messages:

app/models.py: Private messages support in User model.

class User(UserMixin, db.Model):
    # ...
    messages_sent = db.relationship('Message',
                                    backref='author', lazy='dynamic')
    messages_received = db.relationship('Message',
                                        backref='recipient', lazy='dynamic')
    last_message_read_time = db.Column(db.DateTime)

    # ...

    def new_messages(self):
        last_read_time = self.last_message_read_time or datetime(1900, 1, 1)
        return Message.query.filter_by(recipient=self).filter(
            Message.timestamp > last_read_time).count()

The two relationships will return messages sent and received for a given user, and on the Message side of the relationship will add author and recipient back references. The reason why I used a author backref instead of a maybe more appropriate sender is that by using author I can then render these messages using the same logic that I use for blog posts. The last_message_read_time field will have the last time the user visited the messages page, and will be used to determine if there are unread messages, which will all have a timestamp newer than this field. The new_messages() helper method actually uses this field to return how many unread messages the user has. By the end of this chapter I will have this number as a nice badge in the navigation bar at the top of the page.

That completes the database changes, so now it is time to generate a new migration and upgrade the database with it:

(venv) $ flask db migrate -m "private messages"
(venv) $ flask db upgrade

Sending a Private Message

Next I'm going to work on sending messages. I'm going to need a simple web form that accepts the message:

app/main/forms.py: Private message form class.

class MessageForm(FlaskForm):
    message = TextAreaField(_l('Message'), validators=[
        DataRequired(), Length(min=0, max=140)])
    submit = SubmitField(_l('Submit'))

And I also need the HTML template that renders this form on a web page:

app/templates/send_message.html: Send private message HTML template.

{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}

{% block app_content %}
    <h1>{{ _('Send Message to %(recipient)s', recipient=recipient) }}</h1>
    <div class="row">
        <div class="col-md-4">
            {{ wtf.quick_form(form) }}
{% endblock %}

Next I'm going to add a new /send_message/<recipient> route to handle the actual sending of the private message:

app/main/routes.py: Send private message route.

from app.main.forms import MessageForm
from app.models import Message

# ...

@bp.route('/send_message/<recipient>', methods=['GET', 'POST'])
def send_message(recipient):
    user = User.query.filter_by(username=recipient).first_or_404()
    form = MessageForm()
    if form.validate_on_submit():
        msg = Message(author=current_user, recipient=user,
        flash(_('Your message has been sent.'))
        return redirect(url_for('main.user', username=recipient))
    return render_template('send_message.html', title=_('Send Message'),
                           form=form, recipient=recipient)

I think the logic in this view function should be mostly self-explanatory. The action of sending a private message is simply carried out by adding a new Message instance to the database.

The last change that ties everything together is the addition of a link to the above route in the user profile page:

app/templates/user.html: Send private message link in user profile page.

                {% if user != current_user %}
                    <a href="{{ url_for('main.send_message',
                                        recipient=user.username) }}">
                        {{ _('Send private message') }}
                {% endif %}

Viewing Private Messages

The second big part of this feature is the viewing of private messages. For that I'm going to add another route at /messages that works in a fairly similar way to the index and explore pages, including full support for pagination:

app/main/routes.py: View messages route.

def messages():
    current_user.last_message_read_time = datetime.utcnow()
    page = request.args.get('page', 1, type=int)
    messages = current_user.messages_received.order_by(
            page=page, per_page=current_app.config['POSTS_PER_PAGE'],
    next_url = url_for('main.messages', page=messages.next_num) \
        if messages.has_next else None
    prev_url = url_for('main.messages', page=messages.prev_num) \
        if messages.has_prev else None
    return render_template('messages.html', messages=messages.items,
                           next_url=next_url, prev_url=prev_url)

The first thing I do in this view function is update the User.last_message_read_time field with the current time. This is basically marking all the messages that were sent to this user as read. Then I'm querying the Message model for the list of messages, sorted by timestamp from newer to older. I decided to reuse the POSTS_PER_PAGE configuration item here since the pages with posts and messages are going to look very much alike, but of course if the pages were to diverge, it may make sense to add a separate configuration variable for messages. The pagination logic is identical to what I used for posts, so this should all be familiar to you.

The view function above ends by rendering a new /app/templates/messages.html template file, which you can see below:

app/templates/messages.html: View messages HTML template.

{% extends "base.html" %}

{% block app_content %}
    <h1>{{ _('Messages') }}</h1>
    {% for post in messages %}
        {% include '_post.html' %}
    {% endfor %}
    <nav aria-label="...">
        <ul class="pager">
            <li class="previous{% if not prev_url %} disabled{% endif %}">
                <a href="{{ prev_url or '#' }}">
                    <span aria-hidden="true">&larr;</span> {{ _('Newer messages') }}
            <li class="next{% if not next_url %} disabled{% endif %}">
                <a href="{{ next_url or '#' }}">
                    {{ _('Older messages') }} <span aria-hidden="true">&rarr;</span>
{% endblock %}

Here I resorted to another little trick. I noticed that Post and Message instances have pretty much the same structure, with the exception that Message gets an extra recipient relationship (that I don't need to show in the messages page, since it is always the current user). So I decided to reuse the app/templates/_post.html sub-template to also render private messages. For this reason, this template uses the strange for-loop for post in messages, so that all the references to post in the sub-template work with messages too.

To give users access to the new view function, the navigation page gets a new "Messages" link:

app/templates/base.html: Messages link in navigation bar.

                    {% if current_user.is_anonymous %}
                    {% else %}
                        <a href="{{ url_for('main.messages') }}">
                            {{ _('Messages') }}
                    {% endif %}

The feature is now complete, but as part of all these changes there were some new texts that were added in a few places, and those need to be incorporated into the language translations. The first step is to update all the language catalogs:

(venv) $ flask translate update

Then each of the languages in app/translations need to have its messages.po file updated with the new translations. You can find the Spanish translations in the GitHub repository for this project or in the download zip file.

Static Message Notification Badge

Now the private messages feature is implemented, but of course there is nothing that tells a user that there are private messages waiting to be read. The simplest implementation of a navigation bar indicator can be rendered as part of the base template, using a Bootstrap badge widget:

app/templates/base.html: Static message count badge in navigation bar.

                        <a href="{{ url_for('main.messages') }}">
                            {{ _('Messages') }}
                            {% set new_messages = current_user.new_messages() %}
                            {% if new_messages %}
                            <span class="badge">{{ new_messages }}</span>
                            {% endif %}

Here I'm invoking the new_messages() method I added to the User model above directly from the template, and storing that number in a new_messages template variable. Then if that variable is non-zero, I just add the badge with the number next to the Messages link. Here is how this looks on the page:

Messages Badge

Dynamic Message Notification Badge

The solution presented in the previous section is a decent and simple way to show a notification, but it has the disadvantage that the badge only appears when a new page is loaded. If the user spends a long time reading the content on one page without clicking on any links, new messages that come during that time will not be shown until the user finally does click on a link and loads a new page.

To make this application more useful to my users, I want the badge to update the count of unread messages on its own, without the user having to click on links and load new pages. One problem with the solution from the previous section is that the badge is only rendered to the page when the message count at the time the page loaded was non-zero. What's really more convenient is to always include the badge in the navigation bar, and mark it as hidden when the message count is zero. This would make it easy to make the badge visible using JavaScript:

app/templates/base.html: A JavaScript friendly unread messages badge.

                        <a href="{{ url_for('main.messages') }}">
                            {{ _('Messages') }}
                            {% set new_messages = current_user.new_messages() %}
                            <span id="message_count" class="badge"
                                  style="visibility: {% if new_messages %}visible
                                                     {% else %}hidden {% endif %};">
                                {{ new_messages }}

With this version of the badge, I always include it, but the visibility CSS property is set to visible when new_messages is non-zero, or hidden if it is zero. I also added an id attribute to the <span> element that represents the badge, to make it easy to address this element using a $('#message_count') jQuery selector.

Next, I can code a short JavaScript function that updates this badge to a new number:

app/templates/base.html: Static message count badge in navigation bar.

{% block scripts %}
        // ...
        function set_message_count(n) {
            $('#message_count').css('visibility', n ? 'visible' : 'hidden');
{% endblock %}

This new set_message_count() function will set the number of messages in the badge element, and also adjust the visibility so that the badge is hidden when the count is 0 and visible otherwise.

Delivering Notifications to Clients

What remains now is to add a mechanism by which the client receives periodic updates regarding the number of unread messages the user has. When one of these updates occur, the client will call the set_message_count() function to make the update known to the user.

There are actually two methods for the server to deliver these updates to the client, and as you can probably guess, both have pros and cons, so which one to choose is largely dependent on the project. In the first approach, the client periodically asks the server for updates by sending an asynchronous request. The response from this request is a list of updates, which the client can use to update different elements of the page such as the unread message count badge. The second approach requires a special type of connection between the client and the server that allows the server to freely push data to the client. Note that regardless of the approach, I want to treat notifications as generic entities, so that I can extend this framework to support other types of events besides the unread messages badge.

The biggest thing the first solution has is that it is easy to implement. All I need to do is add yet another route to the application, say /notifications, which returns a JSON list of notifications. The client application then goes through the list of notifications and applies the necessary changes to the page for each one. The disadvantage of this solution is that there is going to be a delay between the actual event and the notification for it, because the client is going to request the list of notifications at regular intervals. For example, if the client is asking for notifications every 10 seconds, a notification can be received up to 10 seconds late.

The second solution requires changes at the protocol level, because HTTP does not have any provisions for a server to send data to the client without the client asking. By far the most common way to implement server initiated messages is by extending the server to support WebSocket connections in addition to HTTP. WebSocket is a protocol that unlike HTTP, establishes a permanent connection between the server and the client. The server and the client can both send data to the other party at any time, without the other side asking for it. The advantage of this mechanism is that whenever an event that is of interest to the client occurs, the server can send a notification, without any delays. The disadvantage is that WebSocket requires a more complicated setup than HTTP, because the server needs to maintain a permanent connection with each and every client. Imagine that a server that, for example, has four worker processes can typically serve a few hundred HTTP clients, because connections in HTTP are short lived and are constantly being recycled. The same server would be able to handle just four WebSocket clients, which in the vast majority of cases is going to be insufficient. It is for this limitation that WebSocket applications are typically designed around asynchronous servers, because these servers are more efficient at managing a large number of workers and active connections.

The good news is that regardless of the method that you use, in the client you will have a callback function that will be invoked with the list of updates. So I could start with the first solution, which is much easier to implement, and later, if I find it insufficient, migrate to a WebSocket server, which can be configured to invoke the same client callback. In my opinion, for this type of application the first solution is actually acceptable. A WebSocket based implementation would be useful for an application that requires updates to be delivered with near zero-latency.

In case you are curious, Twitter also uses the first approach for their navigation bar notifications. Facebook uses a variation of it called long polling, which addresses some of the limitations of straight polling while still using HTTP requests. Stack Overflow and Trello are two sites that implement WebSocket for their notifications. You can find what type of background activity occurs on any site by looking in the Network tab of the browser's debugger.

So let's go ahead and implement the polling solution. First, I'm going to add a new model to keep track of notifications for all users, along with a relationship in the user model.

app/models.py: Notification model.

import json
from time import time

# ...

class User(UserMixin, db.Model):
    # ...
    notifications = db.relationship('Notification', backref='user',

    # ...

class Notification(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), index=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    timestamp = db.Column(db.Float, index=True, default=time)
    payload_json = db.Column(db.Text)

    def get_data(self):
        return json.loads(str(self.payload_json))

A notification is going to have a name, an associated user, a Unix timestamp and a payload. The timestamp gets its default value from the time.time() function. The payload is going to be different for each type of notification, so I'm writing it as a JSON string, as that will allow me to write lists, dictionaries or single values such as numbers or strings. I added the get_data() method as a convenience, so that the caller doesn't have to worry about the JSON deserialization.

These changes need to be included in a new database migration:

(venv) $ flask db migrate -m "notifications"
(venv) $ flask db upgrade

As a matter of convenience, I'm going to add the new Message and Notification models to the shell context, so that when I start a shell with the flask shell command, the model class is automatically imported for me:

microblog.py: Add Message model to shell context.

# ...
from app.models import User, Post, Notification, Message

# ...

def make_shell_context():
    return {'db': db, 'User': User, 'Post': Post, 'Message': Message,
            'Notification': Notification}

I'm also going to add a add_notification() helper method in the user model to make it easier to work with these objects:

app/models.py: Notification model.

class User(UserMixin, db.Model):
    # ...

    def add_notification(self, name, data):
        n = Notification(name=name, payload_json=json.dumps(data), user=self)
        return n

This method not only adds a notification for the user to the database, but also ensures that if a notification with the same name already exists, it is removed first. The notification I'm going to work with is going to be called unread_message_count. If the database already has a notification with this name with, for example, a value of 3, whenever the user receives a new message and the message count goes to 4 I want to replace the old notification.

In any place where the unread message count changes, I need to call add_notification() so that I have my notifications for the user updated. There are two places where this changes. First, when the user receives a new private message, in the send_message() view function:

app/main/routes.py: Update user notification.

@bp.route('/send_message/<recipient>', methods=['GET', 'POST'])
def send_message(recipient):
    # ...
    if form.validate_on_submit():
        # ...
        user.add_notification('unread_message_count', user.new_messages())
        # ...
    # ...

The second place where I need to notify the user is when the user goes to the messages page, at which point the unread count goes back to zero:

app/main/routes.py: View messages route.

def messages():
    current_user.last_message_read_time = datetime.utcnow()
    current_user.add_notification('unread_message_count', 0)
    # ...

Now that all the notifications for users are maintained in the database, I can add a new route that the client can use to retrieve notifications for the logged in user:

app/main/routes.py: Notifications view function.

from app.models import Notification

# ...

def notifications():
    since = request.args.get('since', 0.0, type=float)
    notifications = current_user.notifications.filter(
        Notification.timestamp > since).order_by(Notification.timestamp.asc())
    return jsonify([{
        'name': n.name,
        'data': n.get_data(),
        'timestamp': n.timestamp
    } for n in notifications])

This is a fairly simple function that returns a JSON payload with a list of notifications for the user. Each notification is given as a dictionary with three elements, the notification name, the additional data that pertains to the notification (such as the message count), and the timestamp. The notifications are delivered in the order they were created, from oldest to newest.

I do not want clients to get repeated notifications, so I'm giving them the option to only request notifications since a given time. The since option can be included in the query string of the request URL, with the unix timestamp of the starting time, as a floating point number. Only notifications that occurred after this time will be returned if this argument is included.

The final piece to complete this feature is to implement the actual polling in the client. The best place to do this is in the base template, so that all pages automatically inherit the behavior:

app/templates/base.html: Polling for notifications.

{% block scripts %}
        // ...
        {% if current_user.is_authenticated %}
        $(function() {
            var since = 0;
            setInterval(function() {
                $.ajax('{{ url_for('main.notifications') }}?since=' + since).done(
                    function(notifications) {
                        for (var i = 0; i < notifications.length; i++) {
                            if (notifications[i].name == 'unread_message_count')
                            since = notifications[i].timestamp;
            }, 10000);
        {% endif %}

This function is enclosed in a template conditional, because I want to poll for new messages only when the user is logged in. For users that are not logged in, this function will not be included.

You've already seen jQuery's $(function() { ...}) pattern in Chapter 20. This is how you register a function to execute after the page loads. For this feature, what I need to do on page load is to set up a regular timer that gets the notifications for the user. You've also seen the setTimeout() JavaScript function, which runs the function given as an argument after the specific time passes. The setInterval() function uses the same arguments as setTimeout(), but instead of firing the timer just once, it keeps calling the callback function at regular intervals. In this case my interval is set to 10 seconds (given in milliseconds), so I'm going to see the badge update with a resolution of roughly six times per minute.

The function associated with the interval timer issues an Ajax request for the new notifications route, and in its completion callback just iterates over the list of notifications. When a notification with name unread_message_count is received, the message count badge is adjusted by calling the function defined above with the count given in the notification.

The way I'm handling the since argument might be confusing. I start by initializing this argument to 0. The argument is always included in the request URL, but I can't generate the query string using Flask's url_for() like I've done before, because url_for() runs in the server once, and I need the since argument to dynamically update. The first time, the request is going to be sent to /notifications?since=0, but as soon as I receive a notification, I update since to its timestamp. This ensures that I don't receive duplicates, since I'm always asking to receive notifications that occurred since the last notification I've seen. It's also important to note that I declared the since variable outside of the interval function, because I did not want this to be a local variable, I want the same variable to be used in all invocations.

The easiest way to try this out is to use two different browser. Log in to Microblog on both browsers using different users. Then from one of the browsers send one or more messages to the other user. The other browser's navigation bar should update to show the count of messages that you sent in less than 10 seconds. And when you click on the Messages link the unread message count resets back to zero.


  • #51 laticoda said 2020-01-17T14:25:59Z

    Hi all, i'm looking for 2nd method implementation: websocket !

    I have discovered Crossbar[asyncio, not twisted] / Autobahn which seems wonderfull , any help, any link about it would be very strongly appreciate.

    i'd like to use flask with Blueprint for it, but i also use sqlalchemy ORM, should i use quart instead of flask ? Can i mix classic request with asynchronous request on the same server ? i'd like to use docker around it.

    please help !!!

  • #52 Miguel Grinberg said 2020-01-17T23:50:11Z

    @laticoda: Flask-SocketIO would be easier to implement, and you would not need to switch to asyncio or drop Flask.

  • #53 Mansi said 2020-05-13T17:01:54Z

    Hi Miguel, Thanks for the great tutorial. Why haven't you used the messages_received relationship in the new_message method in the User Model Class?

  • #54 Miguel Grinberg said 2020-05-14T22:30:11Z

    @Mansi: no reason. I could have used the relationship and the query would have ended up being the same.

  • #55 Artur said 2020-06-15T10:33:40Z

    Hello Miguel, I've got a small question.

    Why do you convert an 'int' result of 'new_messages()' method by 'json.dumps(data)' and store it as payload of Notification instance as a 'str' object, and next before sending json object to browser through 'jsonify()' method, again convert it to the 'int' object by 'get_data()' method. I realize a 'payload_json' column of Notification model is a 'db.Text', but if it would be 'db.Integer', is it still OK?

    Unless it comes from the reason that you wanted to prepare 'payload_json' column to be universal and ready for storing other types of data for example other notifications besides of messages?


  • #56 Miguel Grinberg said 2020-06-15T15:39:58Z

    @Artur: the add_notification() is generic and works with JSON data. An int is a valid JSON payload. The conversion to JSON and then back into an int is the price you pay to have a generic notification system that can work for other more complex payloads.

  • #57 vlad said 2020-06-28T17:56:28Z

    Hello Miguel! Got an error like this "TypeError: $.ajax is not a function", Is there some way to fix that? thanks.

  • #58 Miguel Grinberg said 2020-06-28T22:53:23Z

    @vlad: you are not importing jQuery in your base HTML page, or else you are importing a build of jQuery that does not include the Ajax feature.

  • #59 Filipe Bezerra said 2020-08-07T17:11:58Z

    I was struggling with the following code:

    messages_sent = db.relationship('Message', foreign_keys='Message.sender_id', backref='author', lazy='dynamic') messages_received = db.relationship('Message', foreign_keys='Message.recipient_id', backref='recipient', lazy='dynamic')

    There's one gotcha which I had to read the docs to understand and fix one problem within my code because I like to change the table name using the attribute __tablename__, so I was confusing myself passing the incorrect syntax to the foreign_keys argument.

    The docs says:

    When specifying foreign_keys with Declarative, we can also use string names to specify, however it is important that if using a list, the list is part of the string:

    billing_address = relationship("Address", foreign_keys="[Customer.billing_address_id]")

    In this specific example, the list is not necessary in any case as there’s only one Column we need:

    billing_address = relationship("Address", foreign_keys="Customer.billing_address_id")

    I was passing foreign_keys="tablename.fk_attribute", the correct syntax in our case is foreign_keys="ModelClassName.fk_attribute"

  • #60 Jordan said 2020-09-09T05:03:17Z

    Hi Miguel,

    This is a phenomenal tutorial, can't thank you enough.

    I have a question related to organisation of my scripts in base.html

    I'm trying to keep my templates as clean as possible and serve scripts from .js files in the Flask app/static directory.

    This works fine for the hover logic and translate logic (hover.js, translate.js), and I can call them effectively with the script tag:


    {% block scripts %} ... <script type="text/javascript" src="{{ url_for('static', filename = 'translate.js') }}"></script> <script type="text/javascript" src="{{ url_for('static', filename = 'hover.js') }}"></script>


    I'm having no such luck with the polling notifications.

    If I include the actual JS in the base.html page, new notifications show as expected:

    "GET /notifications?since=1599627039.5749574 HTTP/1.1" 200 -

    If I include a link to the JS file, using url_for, the GET requests trigger successfully, but the [since] variable remains fixed at 0 in the HTTP request, and the notifications do not update for the user (I can see this in the console):

    "GET /notifications?since=0 HTTP/1.1" 200 - "GET /notifications?since=0 HTTP/1.1" 200 - "GET /notifications?since=0 HTTP/1.1" 200 - "GET /notifications?since=0 HTTP/1.1" 200 -

    Can you explain what I'm missing here?

    Thank you again

  • #61 Miguel Grinberg said 2020-09-09T14:54:41Z

    @Jordan: The only thing I can tell you is that you have a bug in how you extracted this function into its new file, but without seeing what you've done I cannot tell you exactly what the bug is.

  • #62 Kieron Spearing said 2020-09-18T10:26:38Z

    Hey Miguel

    When doing this part I cam up with a few questions.

    1) When implementing a pm service is it best to limit the amount of characters to such a low amount such as 140 or could you allow it to be big ones in order to allow say "announcements" and set them up in separate links like gmail almost? If doing this what would the character limit you would set be? I know its quite different as I am assuming you would need the messages tab to instead of displaying posts display a page of panels with the Subject that links you to the actual page to display the actual message. Or if you wanted to separate messages link to dynamically created conversation links to have different people in separate conversations. I think this is how to do it but basically the main question is what is a reasonable limit to set on the character length?

    2) Would it be better to separate the entire messaging system from the main app as we did in chapter 15 with the error system? If building a larger app with either or both features as I mentioned above would it be better to separate out the private_messaging or announcement system?

    3) With regards to the last section of the tutorial would it be better to make the Ajax call at a faster rate, thus eliminating the delay time? or what would the disadvantages of doing this be?

    I know this is a mouthful but I am curious and if my method of thinking was wrong with regards to the announcement, or individual conversations in private messaging what process would you follow?

  • #63 Miguel Grinberg said 2020-09-18T19:40:23Z

    @Kieron: 1) both approaches are okay. Use the one that makes more sense for your application. 2) It's a matter of preference. I only refactor things into separate package/blueprint when they grow too much and become unmanageable when combine with the rest of the application. 3) The rate needs to be decided for each application. Quicker polling means more traffic on your server.

  • #64 Francisco Pires Costa said 2021-04-18T19:10:19Z

    @Miguel Awesome work as usual! Would like to know if you have any idea on why when i click on private message to another user, nothing is shown. And no errors.

  • #65 Miguel Grinberg said 2021-04-18T22:09:35Z

    @Francisco: with such little information it is impossible for me to know. You are in the best position to debug this. Start from the link that you are clicking. Is it the correct link? If it is, how does the server respond to the link being clicked? And keep going through everything that way. You can also compare your code against mine to find your mistake.

  • #66 Brian said 2021-04-22T00:58:23Z

    Hi Miguel, thank you for this tutorial. Why is it that the new_messages() function in User queries the Message table instead of the messages_received relationship? In general, when is should the relationship be queried instead of the table? For example instead of the current implementation, would it be acceptable to do:

            return self.messages_received.query.filter_by(recipient_id = id).filter(
                messages_received.c.timestamp > last_read_time).count()
  • #67 Francisco Pires Costa said 2021-04-22T02:21:52Z

    @Miguel: Again sorted! :) only needed update the renamed field names, amazing contents, thank you and cheers!

  • #68 Miguel Grinberg said 2021-04-22T10:31:06Z

    @Brian: the relationship is a high-level construct that only exists within SQLAlchemy. In the database the only thing that exists is the foreign key. Querying through a relationship is just a shortcut that achieves the same as querying the foreign key, so both ways are equivalent. Use the one that makes more sense to you.

  • #69 Josheph Malorie said 2021-07-30T14:11:00Z

    Encountering this error : jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endif'. , I mean I literally have been in war with running JavaScript with jinja from the very beginning . As the jinja logic of {% endif %} is inside the script, it is showing that its an unknown tag, so what do I have to do?

    and thanks Mr. Miguel Grienberg for these awesome lessons.

  • #70 Miguel Grinberg said 2021-07-30T16:16:02Z

    @Josheph: I can't really tell you exactly what the problem is, but for sure you have mismatched Jinja tags in your template.

  • #71 Sergey said 2021-09-08T10:08:03Z

    flask db migrate -m "private messages" - this command can see only new. It don't see changes in curent tables. I need to delete data base, make new Initialization and only then migration. After that I need to reload data in data base. It's not comfortable.

  • #72 Miguel Grinberg said 2021-09-08T13:40:50Z

    @Sergey: It's impossible for me to diagnose your problem when you provide no details. Changes in tables should be detected.

  • #73 Gitau Harrison said 2022-01-24T07:19:17Z

    <h3>url_for() in Ajax</h3>

    In the chapter 20 where you explain how to implement some JavaScript to create a popover component, you mention that you cannot user url_for() in JavaScript, hence you explicitly concatenate the request string as:

        '/user' + elem.first().text().trim() + 'popup'

    It makes sense how the username can be extracted in the popover case.

    In this chapter, though, you have used url_for() . Does it mean that this function can still be used in Ajax to make requests to the server?

  • #74 Miguel Grinberg said 2022-01-24T11:40:54Z

    @Gitau: url_for is a Python function. You can use it in any Python code that runs in the server, including inside Jinja templates. You cannot use it on JavaScript code that runs in the client.

  • #75 Martin Karanja said 2022-03-06T07:48:33Z

    Thanks for this great tutorial, it has been a great help. I wanted to inquire on which design pattern is best suited for an application sending many emails. I have in mind an app sending many notifications via SMS and email and I am curious which design pattern is the most effective and is easily maintainable when adding new features to the app?

Leave a Comment