Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 3.58 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Django get static media files
  2. <img src="/media/img/logo.jpg" alt="logo" />
  3.        
  4. # Absolute filesystem path to the directory that will hold user-uploaded files.
  5.  # Example: "/home/media/media.lawrence.com/media/"
  6.  MEDIA_ROOT = '/opt/lab/labsite/media/'
  7.  
  8.  # URL that handles the media served from MEDIA_ROOT. Make sure to use a
  9.  # trailing slash.
  10.  # Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
  11.  MEDIA_URL = ''
  12.  
  13.  # Absolute path to the directory static files should be collected to.
  14.  # Don't put anything in this directory yourself; store your static files
  15.  # in apps' "static/" subdirectories and in STATICFILES_DIRS.
  16.  # Example: "/home/media/media.lawrence.com/static/"
  17.  STATIC_ROOT = ''
  18.  
  19.  # URL prefix for static files.
  20.  # Example: "http://media.lawrence.com/static/"
  21.  STATIC_URL = '/opt/lab/labsite/media/'
  22.        
  23. MEDIA_ROOT = '/opt/lab/labsite/media/'
  24.  
  25.   MEDIA_URL = '/media/'
  26.  
  27.   STATIC_ROOT = '/opt/lab/labsite/static/'
  28.  
  29.   STATIC_URL = '/static/'
  30.  
  31.   STATICFILES_DIRS = (
  32.   )
  33.  
  34.   # List of finder classes that know how to find static files in
  35.   # various locations.
  36.   STATICFILES_FINDERS = (
  37.       'django.contrib.staticfiles.finders.FileSystemFinder',
  38.       'django.contrib.staticfiles.finders.AppDirectoriesFinder',
  39.   #    'django.contrib.staticfiles.finders.DefaultStorageFinder',
  40.   )
  41.  
  42.   TEMPLATE_LOADERS = (
  43.       'django.template.loaders.filesystem.Loader',
  44.       'django.template.loaders.app_directories.Loader',
  45.   #     'django.template.loaders.eggs.Loader',
  46.   )
  47.  
  48.   MIDDLEWARE_CLASSES = (
  49.       'django.middleware.common.CommonMiddleware',
  50.       'django.contrib.sessions.middleware.SessionMiddleware',
  51.       'django.middleware.csrf.CsrfViewMiddleware',
  52.       'django.contrib.auth.middleware.AuthenticationMiddleware',
  53.       'django.contrib.messages.middleware.MessageMiddleware',
  54.   )
  55.  
  56.   ROOT_URLCONF = 'labsite.urls'
  57.  
  58.   # Python dotted path to the WSGI application used by Django's runserver.
  59.   WSGI_APPLICATION = 'labssite.wsgi.application'
  60.  
  61.   TEMPLATE_DIRS = (
  62.       # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
  63.       # Always use forward slashes, even on Windows.
  64.       # Don't forget to use absolute paths, not relative paths.
  65.      '/opt/lab/labsite/templates'
  66.   )
  67.  
  68.   INSTALLED_APPS = (
  69.       'django.contrib.auth',
  70.       'django.contrib.admin',
  71.       'django.contrib.contenttypes',
  72.       'django.contrib.sessions',
  73.       'django.contrib.sites',
  74.       'django.contrib.messages',
  75.       'django.contrib.staticfiles',
  76.       'home',
  77.       # Uncomment the next line to enable admin documentation:
  78.       # 'django.contrib.admindocs',
  79.   )
  80.  
  81.   # A sample logging configuration. The only tangible logging
  82.   # performed by this configuration is to send an email to
  83.   # the site admins on every HTTP 500 error when DEBUG=False.
  84.   # See http://docs.djangoproject.com/en/dev/topics/logging for
  85.   # more details on how to customize your logging configuration.
  86.  
  87.   TEMPLATE_CONTEXT_PROCESSORS = (
  88.      'django.core.context_processors.static',
  89.      'django.contrib.auth.context_processors.auth'
  90.   )
  91.        
  92. <img src="{{ STATIC_URL }}img/hi.png" alt="Hi!" />
  93.  <img src="/static/img/hi.png" alt="Hi!" />
  94.  <img src="/media/img/hi.png" alt="Hi!" />
  95.  <img alt="Hi!" src="/opt/ilabs/ilabs_site/media/img/hi.png">
  96.        
  97. STATIC_ROOT = '/opt/html/static/'
  98. STATIC_URL = '/static/'
  99.  
  100. STATICFILES_DIRS = (
  101.     '/opt/lab/labsite/static/',
  102. )
  103.        
  104. TEMPLATE_CONTEXT_PROCESSORS = (
  105.     ...
  106.     'django.core.context_processors.static',
  107.     ...
  108. )
  109.        
  110. 'django.contrib.staticfiles',
  111.        
  112. <img src="/static/img/logo.jpg" alt="logo" />
  113.        
  114. <img src="{{ STATIC_URL}}img/logo.jpg" alt="logo" />
  115.        
  116. python manage.py collectstatic