Creating Stylish Popovers in Flask with Bootstrap 5 (Using Jinja Templating)

Currently, I am attempting to implement a simple popover feature using a div component within a Jinja template that will be rendered by Flask. Despite my efforts, I am encountering difficulties in getting it to function correctly.

Within my 'layout.jinja2' file, the basic structure of the HTML page is defined:

<!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>
            <script 
                src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.bundle.js" 
                integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" 
                crossorigin="anonymous"></script>
        </body>
    </html>
    

Additionally, I have created another Jinja file, 'page_2.jinja', which extends the layout and includes a navigation bar with the desired dropdown menu:

% 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>
        
        <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 %}
    

Despite setting up the structure for the popover as per Bootstrap documentation, I am encountering an error when trying to enable it using the provided script in 'page_2.jinja':

page_2:67 Uncaught ReferenceError: bootstrap is not defined

At this point, I am unsure of where I am going wrong. Any assistance in resolving this issue would be greatly appreciated. This is the only instance where I require JavaScript on the site, and I opted for Bootstrap 5 to avoid potential conflicts with jQuery, especially since React is used on other pages.

Answer №1

The script that triggers all Popovers in your content jinja block is missing the definition of bootstrap. This issue occurs because Bootstrap is called after the script in your layout.jinja2 file. To fix this error, move the script tag that references Bootstrap to the top of the file, above the jinja block.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Simultaneously sending jQuery.ajax data while submitting a form

I'm facing a bit of a dilemma here. I have a form with multiple fields, including one for entering links. When you input a link and click the add button, the link is added to a link_array using jQuery. My goal is to send this array through the jQuery. ...

Implementing dynamic component rendering and iterating through a list in React JS based on state changes

Trying out React JS for the first time and encountering a couple of issues. 1) Attempting to display a table from a different class upon clicking the show button. However, even when the state is true for showing the table, it doesn't appear. 2) In t ...

Maintain parental visibility with children when navigating to a different page

I am currently working on a vertical accordion menu that opens on hover, stays open, and closes when other items are hovered. The great assistance I received from @JDandChips has been instrumental in getting this feature up and running. Now, my main focus ...

When there is an absence of data, the jQuery datatable mysteriously van

I have encountered an issue in my Django app where a datatable used in the template disappears if there is missing data in any column or row. The problem occurs when trying to download the data in CSV, Excel, PDF, or copy format. Here is the HTML code snip ...

How can I implement BoxHelper on an Object3D containing children in Three.js?

I am exploring the possibility of using the THREE.BoxHelper to generate a bounding box for an Object3D that has children. My goal is to render a wireframe bounding box for the object while omitting diagonals on the box's faces. Upon inspecting the sou ...

The most effective method for adding data to a <pre /> element is through VueJS

I have an electron app that executes a program and stores the output when data is received. My goal is to showcase the content of this output within an html pre element. One approach I can take is to create a variable and continuously add the output data ...

React- Struggling to modify state from child component using function declared within a parent component

This is my main component: import React, {useState} from 'react'; import SearchBar from '../components/SearchBar'; import WeatherDisplay from '../components/WeatherDisplay'; import LocationInfo from '../components/Locat ...

Increase the number of table rows as you scroll downwards

Hello everyone! I've been searching online for information on how to implement infinite scrolling in a table, but all I found were tutorials using divs. Can someone please provide guidance on how to achieve this? Perhaps a sample code snippet or tuto ...

Update the class of the element that is currently selected using jQuery and the `this` keyword

Is there a way to change the class on hover only for the current element using 'this'? The code I have changes classes for all elements, but I need it to work individually. Here is the code snippet I'm currently using: https://codepen.io/ky ...

Upgrade from Bootstrap 3.7 to 5.0.2 on a .NET MVC project causing a System.NullReferenceException bug

I have recently upgraded my .NET MVC Application by uninstalling the basic bootstrap template and installing Bootstrap 5.0.2 from NuGetPackage Manager. However, I am now facing an issue at runtime (the build process is successful) : System.NullReferenceExc ...

Utilizing JADE in conjunction with ng-class

I'm having trouble getting this expression to work properly in my JADE nav-bar. It looks correct to me. li(ng-class="{'active' : true}"): a(href='#') INFO SHEET What am I missing here? The class doesn't get applied as expect ...

What is the method for determining someone's age?

I am trying to extract the date from a field called "DatePicker" and then enter that date into the field labeled "NumericTextBox" Thank you <div> <sq8:DatePicker runat="server" ID="dateDatePicker"> <ClientEvents OnDateSelected="get_curr ...

What could be the reason for the malfunction? JSON, AJAX, and PHP

As a complete beginner in PHP, jQuery, and AJAX, I am trying to create a basic website that allows users to set and display temperature limits using AJAX. My goal is to fetch data from a JSON file and update it with new information upon clicking a button. ...

Ugly consequences arise as Gulp stumbles upon uncharted territory during the uglify

I'm experiencing an issue with my squish-jquery task. Every time I run it, I encounter the following error: Starting 'squish-jquery'... events.js:85 throw er; // Unhandled 'error' event ^ Error at new JS_Par ...

Having trouble replacing using String.replace() in Javascript?

I am working on a project that involves parsing a website and retrieving information from a database. The code I have looks like this: var find = body.match(/\"text\":\"(.*?)\",\"date\"); After running the code, I get the fo ...

Initiate gapi.auth2 upon VueJs initialization

I am currently developing a Vue.js web application that allows users to connect with their Google accounts. The login process, both front-end and back-end, is functioning properly: the Google sign-in button is displayed, the user clicks on it, and their a ...

Guide to incorporating a vanillaJS npm script into a Nuxt plugin

Struggling with saving images directly to AWS S3 using AWS S3. Attempted to add the AWS package as a plugin without success. In the nuxt.config.js file, I have: plugins: [ ... '~plugins/S3.js' ], In the plugins/s3.js file: import vue from ...

The 'wrapper' property is not present in the 'ClassNameMap<never>' type in Typescript

Hey there, I've been encountering a puzzling issue in my .tsx file where it's claiming that the wrapper doesn't exist. My project involves Material UI and Typescript, and I'm relatively new to working with Typescript as well as transiti ...

verify the current version of your web browser

Is there a way for me to detect the browser version being used by a user and prompt them to update it? ...

I'm struggling to make this script replace the values within the table

I am struggling with a script that I want to use for replacing values in a google doc template with data from a google sheet. The script is able to recognize the variables and generate unique file names based on the information from the google sheet. Howev ...