Category: Python

It groups together posts about Python

  • Empty datatable in Python/Django, even if the referred query returns a non-empty set

    Hey, there! How are you doing?

    I guess you’re facing an annoying (but quite easy to solve) problem… am I right? Well, no more talk, let’s go ahead!

    I can bet that the problem is that you didn’t put the desired set of components in a context variable. But what am I talking about?

    See, in your views.py you need to have something like:

    1. Creating the Context in views.py

    @login_required
    def notifica_modelo(request, id):
        modelos = Modelo.objects.all
        notificar_clientes_modelo(id)
        context = {'modelos': modelos}
        return render(request, "pages/envia_notificacoes.html", context)
    

    In that piece of code, the method notifica_modelo stores all the Modelo objects in the variable context, which is just a dictionary containing key-value pairs.

    2. Accessing the Context in the Template

    To improve your understanding, here is the partial template envia_notificacoes.html:

    {% load static %}
    {% load bootstrap_icons %}
    
    <div class="container-lista">
        <h2>Enviar notificações</h2>
        <br>
        <table id="datatable">
            <tr>
                <th>Notificação</th>
                <th>&nbsp;</th>
                <th>&nbsp;</th>
            </tr>
            {% for modelo in modelos %}
            <tr>
                <td>{{ modelo.assunto }}</td>
                <td>
                    <a href="{% url 'exibe_modelo' modelo.id %}">
                        {% bs_icon 'search' color='blue' %}
                    </a>
                </td>
                <td>
                    <a href="{% url 'notifica_modelo' modelo.id %}">
                        {% bs_icon 'send' color='blue' %}
                    </a>
                </td>
            </tr>
            {% endfor %}
        </table>
    </div>
    
    

    By iterating through the variable modelos, the data is retrieved from the context dictionary passed from the view, and everything works smoothly.

    Final Thoughts

    See? Told you it was easy! If you have any questions or want to dive deeper, just drop a comment or message. Happy coding!

    — Igor Magalhães Oliveira

  • How to enable your Redis to be seen and accept connections from everywhere

    Hello, everyone!

    The aim here is to write something as useful as fast for reading and at the end you’ll be able to access your Redis server from everywhere.

    Step 1: search the file redis.conf

    Usually, this file is under /etc/redis directory, if your system runs in a Linux OS, but you can check it using locate command (review this post):

    locate redis.conf
    

    Then, you have to edit it. If you’re using Windows you have to open it in the administrative mode in a tool like Notepad++; if you’re using Linux, you can do (inside the correct directory):

    sudo nano redis.conf
    

    Now, you’ll change the protected-mode parameter and comment the line with bind 127.0.0.1:

    In Linux, press CTRL+O (to save) and CTRL+X (to exit the nano editor), then restart Redis service:

    sudo service redis-service restart
    

    We’re done! Everything will run fine when your system tries to communicate with the Redis server.

    By Igor Magalhães Oliveira