django包含模板中的静态文件?

时间:2015-11-19 10:59:57

标签: python django

伙计们对django说新手...我试图将像图像和css这样的静态文件加载到我的模板中..但它不起作用.. 这是代码

----------- ---------- settings.py

class Course
  has_many :enrollments
  has_many :students, through: :enrollments

  def active
    # check for active and return true or false
  end
end

--------- --------- urls.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/',
)

------ ------的index.html

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)

]

图像和js文件都没有加载..

文件系统如下

{% 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" />

4 个答案:

答案 0 :(得分:1)

你说你有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/'

答案 1 :(得分:1)

加载静态文件的方式有误。以这种方式加载它:

<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')

如果您可以在项目中提供完整的文件夹结构,那么可以告诉您确切的更改。

答案 2 :(得分:0)

将您的静态网址更改为

STATIC_URL = '/static/'

在模板中,在开始

中添加以下行
{% load staticfiles %}

对于整个项目所需的静态文件,您可以将它们放在manage.py所在的同一目录中。对于与特定应用程序绑定的静态文件,您可以在settings.py中使用以下设置。就像你的情况一样

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'hostel_management/static/',

没有必要像你一样在urls.py中包含url。这不适合生产使用。 您也可以通过将模板目录移动到与静态文件目录相同的级别来尝试它。

答案 3 :(得分:0)

将应用程序名称添加到已安装的应用程序[]

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello_world',#add here
)