我有一个使用django模板引擎的非常简单的网站。在这个网站我使用Bootstrap进行设计和基本的js。 bootstrap的css组件完全可以工作,但我不能让js工作:
{% 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
将css和js文件传送到浏览器,因为以下日志将显示,但我也找不到/ 200:
[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
当我点击右边的小栏时,什么都不会发生。 如果我使用bootstrap的cdn一切都会正常工作。
我做错了什么?
答案 0 :(得分:0)
在浏览器控制台中,我们可以确定您的引导程序包存在问题。所以,这与django无关。
您可以在bootstrap.min.js
:
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/popper.min.js"></script>
答案 1 :(得分:0)
先加载jquery-3.5.1.slim.min.js
,然后加载popper.min.js
,然后再加载bootstrap.min.js
。
重新排列它们对我有用
<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>