Django获取静态媒体文件

时间:2012-07-15 07:32:41

标签: django django-admin django-views django-settings

所有,我正在使用Django1.4,我有一个关于访问img文件,js文件等的问题...我的文件在目录/ opt / lab / labsite / media / img中用于图像和/ opt / lab / labsite / media / js用于javascript文件。我应该更改设置文件中的设置,以便下面的html工作..我正在使用django服务器来运行代码

我的代码目前位于/opt/lab/labsite/settings.py,其他模块位于/ opt / lab / labsite / dreams。

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

以下是settings.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">

2 个答案:

答案 0 :(得分:1)

你设置的

STATIC_URL看起来不太好。我认为那应该是STATIC_ROOT

您应该设置(例如)MEDIA_URL='/media/'STATIC_URL='/static/',我希望您还在urls.py中启用了静态文件服务

答案 1 :(得分: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

这会将所有文件从/opt/lab/labsite/static/复制到STATIC_ROOT - /opt/html/static/。这对部署非常有用。

但这一切都是为了发展。对于生产,有更好的方法来提供静态内容 - https://docs.djangoproject.com/en/dev/howto/static-files/#staticfiles-production