我是新来的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