我是新來的Django框架,我不能讓它提供靜態文件。我正在關注網站教程here。模板文件夾正確加載。我不能訪問本地主機:8000 /靜態/django沒有提供靜態文件。 404錯誤
這裏的settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '626+nh90b61sb)[email protected](v!o5g&&()s)[email protected]*ay$tor!$nln88#*'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_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',
'rango',
)
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',
)
ROOT_URLCONF = 'tangoWithDjangoProject.urls'
WSGI_APPLICATION = 'tangoWithDjangoProject.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/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.7/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.7/howto/static-files/
STATIC_URL = '/static/'
STATIC_PATH = os.path.join(BASE_DIR, 'static')
STATICFILES_DIR = (
STATIC_PATH,
)
這裏的index.html的,我想加載圖像
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rango Index</title>
</head>
<body>
<h1>Rango Index page</h1>
<p><strong>{{boldmessage}}</strong></p>
<p><a href="/rango/about/">About</a></p>
<img src="{% static "images/steve.jpg" %}"/>
</body>
</html>
不應該是{%load staticfiles%} ?? –
我試過了,但它不起作用 –
你還需要'STATIC_URL ='/ static /''並指向你的靜態文件夾。模板路徑是您的html模板的路徑。 ... https://docs.djangoproject.com/en/1.8/howto/static-files/ – WayBehind