2015-07-19 98 views
0

我是新來的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> 
+0

不應該是{%load staticfiles%} ?? –

+0

我試過了,但它不起作用 –

+0

你還需要'STATIC_URL ='/ static /''並指向你的靜態文件夾。模板路徑是您的html模板的路徑。 ... https://docs.djangoproject.com/en/1.8/howto/static-files/ – WayBehind

回答

0

settings.py

import os 
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__) + '/../') 

STATIC_URL = '/static/' 
STATIC_ROOT = PROJECT_DIR + '/static/' 
MEDIA_URL = '/media/' 
MEDIA_ROOT = PROJECT_DIR + '/media/' 

urls.py

from django.conf import settings 
from django.conf.urls.static import static 

if settings.DEBUG: 
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 
    urlpatterns += static('/templates/', document_root=settings.PROJECT_DIR + '/templates/') 
+0

但是當我打印BASE_DIR時,它是一個空字符串。我究竟做錯了什麼? –

+0

show ypur traceback –

+0

如果我將我的靜態文件夾移動到django-app文件夾中,原始代碼就可以正常工作。我可以在別處與你分享代碼嗎?這是一段很長的代碼。 –