2012-07-15 83 views
1

所有,我使用Django1.4,我有一個關於訪問img文件,js文件等的問題...我有我的文件在目錄/ opt/lab/labsite/media/img圖像和/ opt /實驗室/ labsite /媒體/ js的javascript文件。我應該如何更改設置文件中的設置,使下面的html工作..我使用django服務器運行代碼Django獲取靜態媒體文件

我的代碼目前位於/opt/lab/labsite/settings.py和其他模塊位於在/ opt /實驗室/ 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/',我希望你也能靜態文件urls.py服務

+0

什麼應該是media_root ????? – Rajeev 2012-07-15 07:58:02

+0

我錯過了urls.py中的設置並解決了問題。謝謝 – Rajeev 2012-07-15 08:40:35

1

settings.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', 

然後:

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

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

如果logo.jpg位於/opt/lab/labsite/static/img/

此外,STATIC_ROOT作爲靜態文件夾由Web服務器提供服務/反向像Apache,Nginx等代理如果你調用:

python manage.py collectstatic 

這會將所有文件從複製到STATIC_ROOT - /opt/html/static/。這對部署很有用。

但這一切都是爲了發展。對於製作有更好的方法來提供靜態內容 - https://docs.djangoproject.com/en/dev/howto/static-files/#staticfiles-production

+0

什麼應該是media_root – Rajeev 2012-07-15 08:06:28

+0

如上所述:'#將保存用戶上傳文件的目錄的絕對文件系統路徑。「 - 這是上傳目錄,具有相應的權限。這應該與STATIC_URL/STATIC_ROOT不同,因爲它假定文件將被上傳到那裏。所以你可以相應地設置它,以防你真的有這樣的功能來上傳內容。否則 - 只需將其留空即可。要使用它 - 你還需要'django.core.context_processors.media'模板處理器。 – Tisho 2012-07-15 08:09:14

+0

沒有像django 1.4中的TEMPLATE_CONTEXT_PROCESSORS這樣的設置,我應該包括它... – Rajeev 2012-07-15 08:09:35

相關問題