2012-07-15 3 views
1

모두, Django1.4를 사용하고 있습니다. img 파일, js 파일 등에 액세스하는 것과 관련하여 질문이 있습니다 .../opt/lab/labsite/media/img 디렉토리에 파일이 있습니다. 이미지 및/opt/lab/labsite/media/js javascript 파일입니다. 설정 파일의 설정을 변경해야합니다. 아래 HTML이 작동합니다. 코드를 실행하려면 장고 서버를 사용합니다.Django는 정적 미디어 파일을 얻습니다.

내 코드 현재 /opt/lab/labsite/settings.py에있는 다른 모듈/옵션/연구소/labsite에서/... ... 다음은

<img src="/media/img/logo.jpg" alt="logo" /> 

는 설정에서 설정입니다 자리 잡고있다. py 파일

# Absolute filesystem path to the directory that will hold user-uploaded files. 
# Example: "/home/media/media.lawrence.com/media/" 
MEDIA_ROOT = '/opt/lab/labsite/media/' 

# URL that handles the media served from MEDIA_ROOT. Make sure to use a 
# trailing slash. 
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/" 
MEDIA_URL = '' 

# Absolute path to the directory static files should be collected to. 
# Don't put anything in this directory yourself; store your static files 
# in apps' "static/" subdirectories and in STATICFILES_DIRS. 
# Example: "/home/media/media.lawrence.com/static/" 
STATIC_ROOT = '' 

# URL prefix for static files. 
# Example: "http://media.lawrence.com/static/" 
STATIC_URL = '/opt/lab/labsite/media/' 

Changes

MEDIA_ROOT = '/opt/lab/labsite/media/' 

    MEDIA_URL = '/media/' 

    STATIC_ROOT = '/opt/lab/labsite/static/' 

    STATIC_URL = '/static/' 

    STATICFILES_DIRS = (
) 

    # List of finder classes that know how to find static files in 
    # various locations. 
    STATICFILES_FINDERS = (
     'django.contrib.staticfiles.finders.FileSystemFinder', 
     'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
    # 'django.contrib.staticfiles.finders.DefaultStorageFinder', 
) 

    TEMPLATE_LOADERS = (
     'django.template.loaders.filesystem.Loader', 
     'django.template.loaders.app_directories.Loader', 
    #  'django.template.loaders.eggs.Loader', 
) 

    MIDDLEWARE_CLASSES = (
     'django.middleware.common.CommonMiddleware', 
     'django.contrib.sessions.middleware.SessionMiddleware', 
     'django.middleware.csrf.CsrfViewMiddleware', 
     'django.contrib.auth.middleware.AuthenticationMiddleware', 
     'django.contrib.messages.middleware.MessageMiddleware', 
) 

    ROOT_URLCONF = 'labsite.urls' 

    # Python dotted path to the WSGI application used by Django's runserver. 
    WSGI_APPLICATION = 'labssite.wsgi.application' 

    TEMPLATE_DIRS = (
     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
     # Always use forward slashes, even on Windows. 
     # Don't forget to use absolute paths, not relative paths. 
    '/opt/lab/labsite/templates' 
) 

    INSTALLED_APPS = (
     'django.contrib.auth', 
     'django.contrib.admin', 
     'django.contrib.contenttypes', 
     'django.contrib.sessions', 
     'django.contrib.sites', 
     'django.contrib.messages', 
     'django.contrib.staticfiles', 
     'home', 
     # Uncomment the next line to enable admin documentation: 
     # 'django.contrib.admindocs', 
) 

    # A sample logging configuration. The only tangible logging 
    # performed by this configuration is to send an email to 
    # the site admins on every HTTP 500 error when DEBUG=False. 
    # See http://docs.djangoproject.com/en/dev/topics/logging for 
    # more details on how to customize your logging configuration. 

    TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.static', 
    'django.contrib.auth.context_processors.auth' 
) 

HTML :

당신이 좋아 보이지 않는 설정 한
<img src="{{ STATIC_URL }}img/hi.png" alt="Hi!" /> 
<img src="/static/img/hi.png" alt="Hi!" /> 
<img src="/media/img/hi.png" alt="Hi!" /> 
<img alt="Hi!" src="/opt/ilabs/ilabs_site/media/img/hi.png"> 

답변

1

STATIC_URL. 나는 그것이 STATIC_ROOT이어야한다고 생각한다.

당신은 MEDIA_URL='/media/'STATIC_URL='/static/' (예)로 설정해야합니다 그리고 당신은 또한

+0

media_root ????? – Rajeev

+0

urls.py의 설정을 놓쳤다 고해서 문제가 해결되었습니다. 감사합니다. – Rajeev

1

가 settings.py이 설정 urls.py에서 봉사 정적 파일을 사용하도록 설정 한 희망 :

STATIC_ROOT = '/opt/html/static/' 
STATIC_URL = '/static/' 

STATICFILES_DIRS = (
    '/opt/lab/labsite/static/', 
) 

문맥이 추가 프로세서 :

TEMPLATE_CONTEXT_PROCESSORS = (
    ... 
    'django.core.context_processors.static', 
    ... 
) 

INSTALLED_APPS이 잊지 마세요 :

'django.contrib.staticfiles', 

다음 : STATIC_ROOT 정적 폴더 역할

<img src="/static/img/logo.jpg" alt="logo" /> 

또는

<img src="{{ STATIC_URL}}img/logo.jpg" alt="logo" /> 

logo.jpg는 또한

/opt/lab/labsite/static/img/에있는 경우는, 웹 서버에 의해 제공 될/역방향 아파치, Nginx와 같은 프록시. 호출하는 경우 :

모든 파일을 에서 STATIC_ROOT-으로 복사합니다. 이는 배포에 유용합니다.

그러나 이것은 모두 개발 용입니다. 제작을 위해 정적 컨텐츠를 제공하는 더 좋은 방법이 있습니다. - https://docs.djangoproject.com/en/dev/howto/static-files/#staticfiles-production

+0

media_root가 무엇이되어야합니까? – Rajeev

+0

다음과 같이 :'# 사용자가 업로드 한 파일을 저장할 디렉토리에 대한 절대 파일 시스템 경로 '- 업로드를위한 디렉토리입니다. , 해당 권한이 있습니다. 이것은 파일이 업로드 될 것으로 가정되므로 STATIC_URL/STATIC_ROOT과 달라야합니다. 따라서 콘텐츠를 업로드 할 수있는 기능이있는 경우에 대비하여 적절하게 설정할 수 있습니다.그렇지 않으면 - 비워 두십시오. 이것을 사용하려면'django.core.context_processors.media' 템플릿 프로세서가 필요합니다. – Tisho

+0

django 1.4에는 TEMPLATE_CONTEXT_PROCESSORS와 같은 설정이 없습니다. – Rajeev

관련 문제