2016-07-05 5 views
2

내 django 프로젝트에 정적 파일 (css, js 등)을 가져 오려고합니다. 하지만 내가 뭘 하려는지에 상관없이 오류 500을 보여줍니다. 나는 공식 문서를 여기에서 이미 읽었습니다. https://docs.djangoproject.com/en/1.9/howto/static-files/,하지만 난 그 문제를 볼 수 없습니다 : /. 나를 괴롭히는 것은 템플릿 가져 오기가 작동하는 것입니다.Django 정적 파일이로드되지 않습니다.

dev 콘솔에서도 볼 수있는 GET 요청은 합법적 인 것 같습니다. 예 : "http://127.0.0.1:8000/static/css/custom.css"custom.css는 /project_folder/static/css/custom.css에 있습니다.

내 폴더 구조 : 여기

내 settings.py

myproject 
-> static 
-->css 
--->custom.css 
-> templates 
-->base.html(where i request the static file) 
-> myproject(app) 
-->settings.oy 
-->urls.py 
-> data_handler(app) 
-->static 

..입니다 :

""" 
Django settings for myproject project. 

Generated by 'django-admin startproject' using Django 1.9.7. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.9/topics/settings/ 

For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.9/ref/settings/ 
""" 

import os 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

ALLOWED_HOSTS = [] 


# Application definition 

INSTALLED_APPS = [ 
    'rest_framework', 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'myproject', 
    'info_pages', 
    'data_handler', 
] 

MIDDLEWARE_CLASSES = [ 
    'django.middleware.security.SecurityMiddleware', 
    '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 = 'myproject.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 = 'myproject.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases 

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


# Password validation 
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators 

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', 
    }, 
] 


# Internationalization 
# https://docs.djangoproject.com/en/1.9/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.9/howto/static-files/ 


STATICFILES_DIRS = ( 
    os.path.join(BASE_DIR,'static/'), 
    os.path.join(BASE_DIR,'data_handler/static/'), 
    os.path.join(BASE_DIR,'myproject/static/'), 
    ) 

STATIC_URL = '/static/' 
STATIC_ROOT= os.path.join(BASE_DIR,'static/') 

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

내가 정적 파일 가져 base.html :

{% load staticfiles %} 
<!DOCTYPE html> 

<head> 
    <meta name="generator" content= 
    "HTML Tidy for HTML5 (experimental) for Mac OS X https://github.com/w3c/tidy-html5/tree/c63cc39"> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 

    <title>SMGR: Slime Mold Graph Repository</title> 
    <link href="{% static "css/bootstrap.min.css" %}" rel="stylesheet"> 
    <link href="{% static "css/sticky_footer.css" %}" rel="stylesheet"> 
    <link href="{% static "css/custom.css" %}" rel="stylesheet"> 
    <link href="{% static "css/expandy.css" %}" rel="stylesheet"> 
    <link rel="shortcut icon" type="image/png" href="{% static "favicon/favicon.ico" %}"/> 
    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> 
</head> 

<body> 
    {% block content %} 
    {% endblock content %} 
<div class="spacer-huge"/> 
</body> 

<footer class="footer" style="left:0px;"> 
    <div class="container" style="margin-top:9px"> 
    <a href="http://www.mpi-inf.mpg.de/departments/algorithms-complexity/"> 
     <img src="{% static "images/mpilogo-inf-wide.png" %}" alt="mpi logo"></a> 
    <div style="float:right"> 
     <p class="small"> 
     <a href="http://www.mpi-inf.mpg.de/imprint/" style="font-size:large;">Imprint</a> 
     </p> 
    </div> 
    </div> 
</footer> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> 
</script> <!-- Include all compiled plugins (below), or include individual files as needed --> 
    <script src="{% static "js/bootstrap.min.js" %}"> 
</script> 
+0

또한 URL urls.py에 URL을 추가해야합니다. 이것을 확인하십시오 https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-static-files-during-development –

+0

@RajeshYogeshwar가 맞습니다. 조금 자세하게 설명해 드리겠습니다. DEBUG = 참, 당신의 경우입니다. 그것은 생산의 또 다른 방법입니다 – Bestasttung

+0

고마워요! 나는 내 static.pyiles를 하드 코딩하여 urls.py에 추가 작업을 할 필요가 없도록 settings.py를 변경했습니다. – gdenn

답변

4

장고와 함께 1.9 당신은 그것을하는 두 가지 방법. DEBUG = True이면 서버를 에뮬레이션하는 것이 실행 서버이지만 통계를 제공해야합니다. 앱에서

  • 사용 django.contrib.staticfiles을하고 (목록에서) 좋은 정적 디렉토리와 URL을 설정합니다 그래서 당신은 할 수 있습니다.
  • 프로젝트가 this을 할 당신이 필요합니다 실제 서버에 의해 제공 될 때 단지

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

urlpatterns = [ 
    # ... the rest of your URLconf goes here ... 
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 
을 좋아 루트 URL의 파일에 봉사.

+0

이것은 내 장고 1.11에서 효과가 있었다. – Phares

관련 문제