1

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

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

 <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">
4

2 回答 2

1

STATIC_URL你设置的看起来不太好。我认为应该是STATIC_ROOT

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

于 2012-07-15T07:41:32.417 回答
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

于 2012-07-15T07:59:58.940 回答