Bootstrap 5 Popover with Flask (and Jinja)

Viewed 96

So i am trying to create a simple popover which is a div component in a Jinja template to be rendered by Flask and I cant seem to get it working

layout.jinja2

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.png') }}">
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>{{ title }}</title>
    </head>

    <body class="{{template}}">
        <div class="container">
            {% block content %}
            
            {% endblock %}
        </div>
        <!-- JavaScript Bundle with Popper -->
        <script 
            src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.js" 
            integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" 
            crossorigin="anonymous"></script>
    </body>
</html>

Then I have another jinja file which extents layout that contains my nav bar and the desired dropdown:

page_2.jinja

% extends "layout.jinja2" %}

{% block content %}
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">My Page</a>
    
            <div class="collapse navbar-collapse" id="navbarColor01">
                <ul class="navbar-nav me-auto">
                    {% if current_user.is_authenticated %}
                        {% for app in current_user.app_permissions %}
                            {% if app == 'settings' %} {% continue %} {% endif %}
                            <li class="nav-item">
                                <a class="nav-link active" href="{{ url_for(get_app_url(app)) }}">{{ app.capitalize() }}</a>
                            </li>
                        {% endfor %}
                    {% endif %}
                </ul>
                <ul class="navbar-nav me-right">
                    {% if current_user.is_authenticated %}
                        <div>
                            <img id="menu" src="/assets/user_icon.png" data-bs-toggle="popover" data-bs-trigger="focus" data-bs-content="nav_dropdown">
                        </div>
                    {% else %}
                        <form class="d-flex" method="POST" action="/login">
                            <input class="form-control me-sm-2" type="email" name="email" placeholder="email">
                            <input class="form-control me-sm-2" type="password" name="password" placeholder="password">
                            <button class="btn btn-secondary my-2 my-sm-0" type="submit">Login</button>
                        </form>
                    {% endif %}
                </ul>
            </div>
        </div>
    </nav>
    

    <!-- this is what I want to be displayed when the user clicks on the image in the nav -->
    <div role="tooltip" x-placement="bottom" class="fade show popover bs-popover-bottom" 
        id="nav_dropdown" data-popper-reference-hidden="false" data-popper-escaped="false" 
        data-popper-placement="bottom" style="position: absolute; inset: 0px auto auto 0px; transform: translate(1413px, 52px);">
        
        
        <ul class="list-group">
            <a href="/settings" data-rr-ui-event-key="/settings" class="list-group-item">Settings</a>
            <a href="/logout" data-rr-ui-event-key="/logout" class="list-group-item">Logout</a>
        </ul>
    </div>  

    <script type="text/javascript">
        var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'))
        var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
            return new bootstrap.Popover(popoverTriggerEl)
        })
        
    </script>
{% endblock %}

I see the div where i want the dropdown to be, however its just always there, instead of hidden, and shown when the IMG is clicked.

according to the bootstrap docs:

https://getbootstrap.com/docs/5.0/components/popovers/

I have to enable the popovers using that last script at the bottom of page_2.jinja. however when i run the app, i get the error:

page_2:67 Uncaught ReferenceError: bootstrap is not defined

So i am not really sure what I'm doing wrong here, any and all help would be great, this is the only place i need javascript on the site, and I'm using bootstrap 5 to avoid having to load jQuery since on some other pages i use React and they conflict sometimes.

1 Answers

bootstrap is not defined within the script that activates all Popovers within your content jinja block as Bootstrap is only referenced after it in your layout.jinja2 file. Moving the script tag referencing Bootstrap to the top of that file before the jinja block should resolve this error.

Related