Home > OS >  Bootstrap dropdown list won't work in django html template
Bootstrap dropdown list won't work in django html template

Time:06-27

I am trying to apply bootstrap in my shared html file in a Django project and the dropdown list just won't work I tried everything I could cdn local bootstrap and when I click the dropdown list nothing happens.

Below is the template file:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.min.css' %}">
  
    <title> {% block title %}{% endblock %} </title>
</head>
    <body>
        <nav >
            <a  href="{% url 'home' %}">Home</a>
            <button  type="button" data-toggle="collapse"
            data-target="#navbarCollapse" aria-controls="navbarCollapse"
            aria-expanded="false" aria-label="Toggle navigation">
            <span ></span>
            </button>
            <div  id="navbarCollapse">
                {% if user.is_authenticated %}
                    <ul >
                        <li >
                            <a  href="#" id="userMenu"
                            data-toggle="dropdown" aria-haspopup="true"
                            aria-expanded="false">
                            {{ user.username }}
                            </a>
                            <div 
                                aria-labelledby="userMenu">
                                <a 
                                href="{% url 'upload'%}">Upload</a>
                                <div ></div>
                                <a  href="{% url 'logout' %}">
                                Log Out</a>
                            </div>
                        </li>
                    </ul>
                {% else %}
                        <form >
                        <a href="{% url 'login' %}" >
                        Log In</a>
                        <a href="{% url 'signup' %}" >
                        Sign up</a>
                        </form>
                {% endif %}
            </div>
        </nav>
        <div >
            {% block content %}
            {% endblock content %}
        </div>
        <script src="{% static 'js/bootstrap.bundle.js'%}"></script>
        <script src="{% static 'js/bootstrap.bundle.min.js' %}"></script>
        <script type="text/javascript" src="{% static 'js/bootstrap.min.js' %}"></script>
    </body>
</html>

CodePudding user response:

Use data-bs-toggle='dropdown', refer from boostrap 5.0 docs and make sure that the user is authenticated.

Try this template code:

{% if user.is_authenticated %}
    <ul >
        <li >
            <a  href="#" id="userMenu"
            data-bs-toggle="dropdown" aria-haspopup="true"
            aria-expanded="false">
            {{ user.username }}
            </a>
            <div 
                aria-labelledby="userMenu">
                <a 
                href="#">Upload</a>
                <div ></div>
                <a  href="#">
                Log Out</a>
            </div>
        </li>
    </ul>
    
{% else %}
        <form >
        <a href="#" >
        Log In</a>
        <a href="#" >
        Sign up</a>
        </form>
{% endif %}
  • Related