简体   繁体   中英

problems getting bootstrap.js to work while using Django

I have a very simple site using the django templating engine. in this site i use Bootstrap for design and basic js. the css components from bootstrap totaly work but i cant get js to work:

    {% load staticfiles %}

    <!doctype html>
    <html lang="en">
        <head>
            <title>
                blah
            </title>

            <!-- Required meta tags -->
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

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

        <body>
            <!-- Navbar -->
            <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <a class="navbar-brand" href="/">blah</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
                <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
                <li class="nav-item active">
                    <a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link disabled" href="#">Disabled</a>
                </li>
                </ul>
                <form class="form-inline my-2 my-lg-0">
                <input class="form-control mr-sm-2" type="search" placeholder="Search">
                <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
                </form>
            </div>
            </nav>

            <!-- Space between Navbar and Body -->
            <div class="border-row" style="width:100%; margin-bottom: 15px;"></div>

            <!-- Fullscreen Container -->
            <div class="container-fluid" style="min-height:100%; width:100%;">
                <p>hi</p>
            </div>

            <script src="{% static '/js/jquery.js' %}"></script>
            <script src="{% static '/js/bootstrap.min.js' %}"></script>


        </body>

    </html>

settings.py

"""
Django settings for blah project.

Generated by 'django-admin startproject' using Django 1.11.7.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os

import pymysql
pymysql.install_as_MySQLdb()


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'blah'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['10.0.0.5', 'localhost:8000', 'localhost']


# Application definition

INSTALLED_APPS = [
    'homepage.apps.HomepageConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'blah.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'blah.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#     }
# }

# MySQL
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'blah',
        'USER': 'django',
        'PASSWORD': 'asd',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'de-de'

# TIME_ZONE = 'UTC'
TIME_ZONE = 'Europe/Berlin'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'

screenshot of resulting page screenshot

the css und js files are delivered to the browser as the following log will show, but i also get a /200 not found:

[23/Nov/2017 18:13:53] "GET / HTTP/1.1" 200 14703
[23/Nov/2017 18:13:54] "GET / HTTP/1.1" 200 14703
[23/Nov/2017 18:13:54] "GET /static/css/bootstrap.min.css HTTP/1.1" 200 127343
[23/Nov/2017 18:13:54] "GET /static/css/bootstrap.min.css HTTP/1.1" 200 127343
[23/Nov/2017 18:13:54] "GET /static/js/jquery.js HTTP/1.1" 200 282766
[23/Nov/2017 18:13:54] "GET /static/js/bootstrap.min.js HTTP/1.1" 200 50564
[23/Nov/2017 18:13:54] "GET /static/css/bootstrap.min.css HTTP/1.1" 200 127343
[23/Nov/2017 18:13:54] "GET /static/css/bootstrap.min.css HTTP/1.1" 200 127343
[23/Nov/2017 18:13:55] "GET /static/css/bootstrap.min.css.map HTTP/1.1" 200 514728
Not Found: /200

browser console: browser console

when i click on the little bars on the right nothing will happens. if i use the cdn of bootstrap everything will work fine.

what am i doing wrong?

From your browser console, we can identify there is a problem with your bootstrap bundle. So, this is not related to the django.

You can fix the issue by adding this before bootstrap.min.js :

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/popper.min.js"></script>

Loading jquery-3.5.1.slim.min.js first then popper.min.js and then finally bootstrap.min.js .

Reordering them worked for me

<script type="text/javascript" src="{% static 'bootstrap-4.5.3/js/jquery-3.5.1.slim.min.js'%} "></script>
<script type="text/javascript" src="{% static 'bootstrap-4.5.3/js/popper.min.js'%}"></script>
<script type="text/javascript" src="{% static 'bootstrap-4.5.3/js/bootstrap.min.js'%} "></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM