Having trouble with loading the bootstrap navigation bar in Django? Here's a possible solution you might not have

I'm having an issue with my navigation bar on the home page. The three lines in the navbar do not click, open, or display any links. I believe I need to include some JavaScript code to make this functionality work. Here is a screenshot of the problem: https://i.sstatic.net/vnv8H.png

When those three lines are clicked, they should expand and show the available links. Here is an example of what it should look like: https://i.sstatic.net/6YJnQ.png

Despite researching on Stack Overflow and trying various methods like loading Bootstrap CDN, saving and linking JavaScript files, none of them seem to fix the issue. The CSS files load correctly, but it seems that there might be a problem with how the JavaScript is being handled.

Below is a snippet of the HTML file where the issue arises:

{% load staticfiles %}

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>Album example for Bootstrap</title>
     <link  href="{% static 'css/album.min.css' %}" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link  href="{% static 'css/album.css' %}" rel="stylesheet">

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins)-->
<script href="{% static 'js/jquery.js' %}" ></script>  <!--jquery-->

<!-- <script href = "{% static 'js/collapse.js' %}"></script>

<script href = "{% static 'js/dropdown.js' %}"></script> -->

  </head>

  <body>

    ...
  

In addition, here is a portion from my settings file related to this problem:

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(os.path.abspath(os.path.dirname(__file__)), "static",),
)

Answer №1

  • For those using Django 2.0 and have their static settings functioning correctly, it is important to use {% load static %} instead of {% load staticfiles %}

  • If you need a guide, here is an example that works for static files:

project/settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]

In this setup, there is a folder named 'static' in the root of my project which contains subfolders like 'css', 'js,' etc. Make sure to configure this in your urls.py so that it can be accessed by the template. Here's how I would do it:

project/urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls.static import static        #---imports
from django.conf import settings                  #---imports

urlpatterns = [

path('', user_login),
path('admin/', admin.site.urls), 
] 

urlpatterns += staticfiles_urlpatterns()       # <------- Add this

Then, at the beginning of your HTML page, include the static files

{% load static from staticfiles %}          #<-----this

<link rel="stylesheet" href="{% static 'css/bootstrap.min.css'%}">
<link rel="stylesheet" href="{% static 'css/style.css'%}">

Update

Don't forget to insert bootstrap.bundle.js in your static/js directory. In your base_generic.html, add the following:

<script src="{% static 'js/b.b.js' %}"></script>

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

Having trouble reading the file using jQuery in Internet Explorer 8 and earlier versions due to its non-XML format (albeit resembling XML)

Currently, I am utilizing AJAX to load a KML file (which essentially functions as an XML file). The parsing works seamlessly in IE9, FF, and other browsers, but encounters issues in IE8. Although the data is retrieved, I face difficulties parsing it in jQu ...

Shifting Directive Logic to a Method within a Vue Component

I am currently working with a VueJS component that utilizes the style attribute in the following way: <section :style="{ backgroundImage: src && 'url(' + src + ')' }"> ... <script> export default { props: [& ...

What is the method for determining the overall page load time of a website, taking into account the total loading time instead of just the document.ready()

I recently attempted to create a function using either JavaScript or Python with Selenium to measure the page load time of a website. While document.ready() gives me the DOM load time, it may not capture AJAX calls that occur afterwards. I noticed there i ...

Verify the level of opacity in the image

I'm working on a website that allows users to upload PNG images and save them. However, before they can save the image, I need to check if it contains transparency. Is there a way to determine if an image is not 24-bit using JavaScript? <img id= ...

Easily integrate an HTML form as an input for your Python script

Despite my thorough searches, I haven't been able to find a satisfactory answer. I am creating a local HTML page with Python and would like to extract data from a form on that page, use it as input for a Python script, and then properly display the r ...

Failure of default option to appear in dropdown menu in Angular

I currently have a dropdown list in my HTML setup like this: <select id="universitySel" ng-model="universityValue" ng-options="university._id for university in universities"> <option value="-1">Choose university</option> ...

Verification for duplicated objects in the database is conducted when a user attempts to insert a new object

I have built an application that allows users to enroll in various courses, establishing a many-to-many relationship between users and courses. To prevent users from adding the same course with partially similar values (e.g., same course code and year), I ...

Arrange pictures and div elements in a horizontal layout

I am facing an issue with my code that is displaying 5 'cards' in a messed up layout instead of a straight line. I have tried to align them vertically, but some are still higher and not in the right positions. For reference, I want the layout to ...

How can JavaScript/jQuery be used to update LocalStorage Objects when editing a form?

Having trouble pinpointing an issue with my code. Despite making modifications, the values in localStorage are not updating as expected. Any suggestions on what may be causing this problem? Note: Changing const idx to const i resulted in only the final va ...

Combining strings in Django using unicode characters

Dealing with unicode and string encoding can be quite challenging. I came across a helpful answer on this question that explained how to include special characters (äÄÜ..) in a message. I'm having trouble understanding why version 2 of my code wor ...

Incorporating React's dynamic DIV layout algorithm to generate a nested box view design

My goal is to showcase a data model representation on a webpage, depicting objects, attributes, and child objects in a parent-child hierarchy. I had the idea of developing a versatile React component that can store a single data object while also allowing ...

AngularJS Banner: Displaying Current Calendar Week and Increasing by 10 Days

I'm brand new to Angular and currently encountering some issues. Here's what I'm trying to create: I need to display the current Date: yyyy-MM-ss (Functional) I want to show the current Calendar Week: yyyy-Www (Not Working) When a butto ...

Incorporating a Vue plugin within a different Vue plugin

I am revamping my website by incorporating Vue in the frontend and Django Rest Framework in the backend. To streamline the authentication/headers/error handling for all AJAX calls, I decided to centralize this logic within a shared file. Thus, I created a ...

Leveraging data generated by a CasperJS script within an AngularJS application

After using yeoman.io to create an angular.js single page application, I found myself with app.js managing routes, mycontroller.js scripts, and an index.html file filled with "bower_components" references for libraries installed through the command line us ...

What is the best way to retrieve a specific value in an array object using JavaScript?

I'm working with an array that has the structure shown below: balance = [ {id: 2, is_selected: true}, {id: 4, is_selected: true}, {id: 15, is_selected: false}, {id: 137, is_selected: false}, {id: 30, is_selected: false} ]; Is ther ...

Steps for linking a page to a modal without embedding the page's content within the modal

Here is a snippet of code for a modal (from Twitter Bootstrap) that I am currently working with: <!-- Large Modal --> <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg">Large Modal</button&g ...

There seems to be an issue with Affix functionality in Bootstrap 4 (alpha version)

As detailed in the Bootstrap 3 documentation, I have included the following attributes in a navbar: <nav class="navbar no-margin-bottom" data-spy="affix" data-offset-top="90" > ... </nav> Unfortunately, when scrolling down the page with Boots ...

Can anyone point me to the Angular-2-Release that is located on the https://code.angularjs.org

Word has it that angular 2 has finally made its debut. Personally, I'm not a big fan of relying on npm and bower to automatically load my components. I prefer to keep my dependencies minimal and fully understand each one. Additionally, I like to utili ...

Hunt down the key within the specified array of objects and then proceed to update the value

Exploring the implementation of multiple sorting functionality; the array that holds the field name and sorting order needs to be toggled. Example Click Sort By Name: [{"sortKey":"name","sortValue":"desc"}] Click Sort By Age: [{"sortKey":"name","sortV ...

The ng-model does not bind properly when the input value is set through JavaScript instead of directly from

I'm encountering an issue with a text box in my project. The data is being populated using JavaScript, which reads a QR code and sets the value accordingly. I am using ng-model to bind to a variable in my controller, and while it works perfectly when ...