2017-10-17 163 views
0

我是django的新手。我一直在試圖用django1.11開發一個網站。但是我在某個時候被卡住了。靜態文件不通過模板。任何幫助!靜態文件不在django1.11中加載

settings.py

import os 
 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
 
SECRET_KEY = 'agr8w4(pcdz077#[email protected]%6(evtw((-3g$&$_wf1&[email protected]' 
 
DEBUG = True 
 

 
ALLOWED_HOSTS = [] 
 
INSTALLED_APPS = [ 
 
    'django.contrib.admin', 
 
    'django.contrib.auth', 
 
    'django.contrib.contenttypes', 
 
    'django.contrib.sessions', 
 
    'django.contrib.messages', 
 
    'django.contrib.staticfiles', 
 
    'photo', 
 
] 
 

 
MIDDLEWARE = [ 
 
    'django.middleware.security.SecurityMiddleware', 
 
    'django.contrib.sessions.middleware.SessionMiddleware', 
 
    'django.middleware.common.CommonMiddleware', 
 
    'django.middleware.csrf.CsrfViewMiddleware', 
 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
 
    'django.contrib.messages.middleware.MessageMiddleware', 
 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
 
] 
 

 
ROOT_URLCONF = 'pro.urls' 
 

 
TEMPLATES = [ 
 
    { 
 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
 
     'DIRS': [os.path.join(BASE_DIR,"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 = 'pro.wsgi.application' 
 

 

 
DATABASES = { 
 
    'default': { 
 
     'ENGINE': 'django.db.backends.sqlite3', 
 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
 
    } 
 
} 
 

 

 
AUTH_PASSWORD_VALIDATORS = [ 
 
    { 
 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
 
    }, 
 
    { 
 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
 
    }, 
 
    { 
 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
 
    }, 
 
    { 
 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
 
    }, 
 
] 
 

 

 
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.11/howto/static-files/ 
 

 
STATIC_URL = '/static/' 
 

 
STATIC_ROOT = os.path.join(BASE_DIR, "static", "static_root") 
 

 
STATICFILES_DIRS = [ 
 
os.path.join(BASE_DIR, "static", "our_static"), 
 
] 
 

 
STATICFILES_FINDERS = (
 
    'django.contrib.staticfiles.finders.FileSystemFinder', 
 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder' 
 
    ) 
 

 
MEDIA_URL ='/media/' 
 
MEDIA_ROOT =os.path.join(BASE_DIR, "media_root")

urls.py

from django.conf import settings 
 
from django.conf.urls import include,url 
 
from django.conf.urls.static import static 
 
from django.contrib import admin 
 
from photo.views import home 
 
from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
 
#urlpatterns += staticfiles_urlpatterrens() 
 

 
urlpatterns = [ 
 
    url(r'^$',home,name='home'), 
 
    url(r'^admin/', admin.site.urls), 
 
] 
 

 
if settings.DEBUG: 
 
\t urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 
 
\t urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

views.py

from django.shortcuts import render 
 
def home(request): 
 
\t return render(request,"home.html",{})

home.html做爲

{% load static %} 
 
<!doctype html> 
 
<html lang='en'> 
 
<head> 
 
\t <meta charset="utf-8" /> 
 

 
\t <title>Photography</title> 
 
\t <link rel="stylesheet" href="{% static '/our_static/css/main.css' %}" type="text/css"> 
 
\t 
 
</head> 
 
<body> 
 
\t <h1>Something here</h1> 
 
\t <h2>Anything!</h2> 
 
\t <section> 
 
\t \t <div> 
 
\t \t \t <nav> 
 
\t \t \t \t <ul> 
 
\t \t \t \t \t <li><a href='#home'>Home</a></li> 
 
\t \t \t \t \t <li><a href='#aboutus'>AboutUs</a></li> 
 
\t \t \t \t \t <li><a href='#portfolio'>Portfolio</a></li> 
 
\t \t \t \t \t <li><a href='#services'>Services</a></li> 
 
\t \t \t \t \t <li><a href='#contact'>Contact</a></li> 
 

 
\t \t \t \t </ul> 
 
\t \t \t </nav> 
 
\t \t </div> 
 
\t </section> 
 
\t <img src= "{% static '/our_static/img/a.jpg' %}" height="500" width="500" > 
 

 
\t <footer> 
 
\t \t &copy;[email protected] All rights reserved. 
 
\t </footer> 
 
</body> 
 
</html>

的main.css

*{ 
 
\t margin: 0px; 
 
\t padding: 0px; 
 
} 
 

 
header, nav, footer { 
 
\t display: block; 
 
} 
 

 
body{ 
 
\t text-align: center; 
 
} 
 

 
header{ 
 
\t color: #777777; 
 
}

目錄結構是這樣的!

folder structure

python manage.py collectstatic工作正常,沒有任何錯誤,並且文件被複制到static_root爲好。但它運行時沒有加載頁面python manage.py runserver 這是我運行它時得到的。

enter image description here

回答

1

從URL中移除our_static。當static標籤取代它時不需要。

<link rel="stylesheet" href="{% static 'css/main.css' %}" type="text/css"> 
<img src= "{% static 'img/a.jpg' %}" height="500" width="500" > 
+1

謝謝你。工作! –

+0

@JoanKennedy很高興我能幫忙=) –