[英]django include static files in templates?
伙計們,我是 django 的新手。
-----------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
你說你有script.js
但你試圖得到scripts.js
。
{% load static %}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="{% static 'script.js' %></script>
<img src="{% static "large-image.jpg" %}" alt="img" />
此外,您必須將static
文件夾移動到您的manage.py
所在的位置並設置STATIC_URL = '/static/'
加載靜態文件的方式有誤。 以這種方式加載它:
<link rel="stylesheet" href="{% static "script.js" %}">
我認為你的 settings.py 有錯誤
您加載靜態文件的方式是:
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),"/var/www/static/',
這是根據django docs的方式,
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
如果您可以在項目中提供完整的文件夾結構,則可以告訴您確切的更改。
將您的靜態網址更改為
STATIC_URL = '/static/'
在您的模板中,將以下行放在開頭
{% load staticfiles %}
對於整個項目中需要的靜態文件,您可以將它們放在與 manage.py 相同的目錄中。 對於綁定到特定應用程序的靜態文件,您可以在 settings.py 中使用以下設置。 就像你的情況
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'hostel_management/static/',
)
無需像您所做的那樣在 urls.py 中包含 url。 這不適合生產使用。 您也可以通過將模板目錄移動到與靜態文件目錄相同的級別來嘗試。
將應用名稱添加到已安裝的應用中[]
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello_world',#add here
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.