2017-10-17 5 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はうれしいです=) –

関連する問題