django是否在模板中包含静态文件?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django是否在模板中包含静态文件?相关的知识,希望对你有一定的参考价值。
家伙,我是django的新手。。我试图将图像和CSS之类的静态文件加载到模板中。这是代码
----------- settings.py ----------
"""
Generated by 'django-admin startproject' using Django 1.8.2.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
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.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '#####################################'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'hostel_management.urls'
TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['hostel_management/templates/'],
'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 = 'hostel_management.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES =
'default':
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, javascript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = 'hostel_management/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/var/www/static/',
)
--------- urls.py ---------
from django.conf.urls import include, url
from django.contrib import admin
from views import index
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^index/$', index)
]
-------- index.html ------
% load static from staticfiles %
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="% static "scripts.js" %></script>
<img src="% static "large-image.jpg" %" alt="img" />
图像和js文件均未加载。
文件系统如下
hostel_management
├── manage.py
├──hostel_management
| ├──templates
├──static
| ├──large_image.jpg
| ├──script.js
家伙,我是到django的新手。。我试图将图像和CSS之类的静态文件加载到我的模板中。但是它不起作用。 ---------“”“由'...
答案
您说您有script.js
,但您尝试获得scripts.js
。
另一答案
加载静态文件的方式有误。这样加载:
另一答案
将静态网址更改为
另一答案
将应用程序名称添加到已安装的应用程序[]
以上是关于django是否在模板中包含静态文件?的主要内容,如果未能解决你的问题,请参考以下文章
Django 模板中包含的 Pygal svg 图表的工具提示问题
如何在单独的 js 文件中使用访问 django 模板变量,而不是在同一个 html 文件中包含小脚本块?
如何在通过 pip 安装的应用程序中包含 django 模板?
在实际应用程序目录中包含模板/静态文件的 pip 安装(不仅仅是在站点包目录中)