0

我正在尝试为已升级到 Django 1.4 的 Django 加载静态内容。该项目已成功部署,但我无法找到该项目的图像和所有静态内容。

请找到 settings.py 文件

# Django settings for DataEntry project.
import sys
import os
from path import path

SETTINGS_FILE_FOLDER = path(__file__).parent.abspath()

sys.path.append(SETTINGS_FILE_FOLDER.joinpath("libs").abspath())


DEBUG = True
TEMPLATE_DEBUG = DEBUG
INTERNAL_IPS = ("127.0.0.1", "localhost", "192.168.100.102")

ADMINS = (
    (" Hello World ", "hello.world@gmail.com"),
)
MANAGERS = ADMINS



DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'gototest',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': 'root',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
        'OPTIONS': {
               "init_command": "SET storage_engine=INNODB",
        }
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Asia/Calcutta'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
#MEDIA_ROOT = os.path.join(SETTINGS_FILE_FOLDER,'static')
MEDIA_ROOT = '/static'
STATIC_PATH = '/static'
UPLOAD_DIR =  '/Users/iceman/Documents/gototest/qbank/static/uploads'
SITE_NAME = 'demo.com'
SITE_URL = 'http://alphadev.demo.com'

AUTH_PROFILE_MODULE = 'core.UserProfile'
LOGIN_REDIRECT_URL = '/students/login/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''
#STATIC_ROOT = '/Users/iceman/Documents/gototest/qbank/static'
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = ' hidden '

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
#    'django.template.loaders.filesystem.load_template_source',
#    'django.template.loaders.app_directories.load_template_source',
     'django.template.loaders.filesystem.Loader',
     'django.template.loaders.app_directories.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.gzip.GZipMiddleware',
    #'utils.XhtmlMortifierMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
#simplCACHE_BACKEND = 'memcached://127.0.0.1:11211/'

ROOT_URLCONF = 'urls'

TEMPLATE_DIRS = (
   SETTINGS_FILE_FOLDER.joinpath("../templates"),
    # 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.
)


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

)

AUTHENTICATION_BACKENDS = (
    'accounts.backends.EmailOrUsernameModelBackend',
    'django.contrib.auth.backends.ModelBackend'
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django.contrib.sites',
    'django.contrib.flatpages',

    'core',
    'tinymce',
    'filebrowser',
    'tagging',
    'tagging_autocomplete',
    'django_extensions',
    'registration',
    'questionmanager',
    'corporate',
)

ADMIN_HASH_SECRET = " "

RECAPTCHA_PUBLIC_KEY = " "
RECAPTCHA_PRIVATE_KEY = " "

SERIALIZATION_MODULES = { 'modeljson' : 'wadofstuff.django.serializers.json' }
SOLR_ROOT = "http://dev.demo.com:8080/QuestionSolr"

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.request",
    "django.core.context_processors.i18n",

    "utils.context_processor",
    "qutils.context_processor",
)

#'plugins': "table,paste,searchreplace,safari,asciimath,contextmenu",

TINYMCE_JS_URL = "/static/tinymce/tiny_mce/tiny_mce.js" 
TINYMCE_JS_ROOT ="/static/tinymce/tiny_mce"
TINYMCE_DEFAULT_CONFIG = {
    'plugins': "table,asciimath,gototest,indicime",
    'mode' : "textareas",
    'theme': "advanced",
    'cleanup_on_startup': True,
    'custom_undo_redo_levels': 10,
    'theme_advanced_buttons1' : "fontselect,fontsizeselect,formatselect,bold,italic,underline,strikethrough,separator,sub,sup,separator,cut,copy,paste,undo,redo",
    'theme_advanced_buttons2' : "justifyleft,justifycenter,justifyright,justifyfull,separator,numlist,bullist,outdent,indent,separator,forecolor",
    'theme_advanced_buttons3' : "gototest,backcolor,separator,hr,link,unlink,image,table,code,separator,asciimath,asciimathcharmap,indicime",

    'theme_advanced_fonts' : "Arial=arial,helvetica,sans-serif,Courier New=courier new,courier,monospace,Georgia=georgia,times new roman,times,serif,Tahoma=tahoma,arial,helvetica,sans-serif,Times=times new roman,times,serif,Verdana=verdana,arial,helvetica,sans-serif",
    'theme_advanced_toolbar_location' : "top",
    'theme_advanced_toolbar_align' : "left",
    'theme_advanced_statusbar_location' : "bottom",
    'tab_focus' : ':prev,:next',

}

#'content_css' : "/static/css/content.css",

TINYMCE_SPELLCHECKER = False
TINYMCE_COMPRESSOR = True

FORCE_LOWERCASE_TAGS = True

MARKITUP_FILTER = ('markdown.markdown', {'safe_mode': True}) 
MARKITUP_SET = 'markitup/sets/markdown'
MARKITUP_SKIN = 'markitup/skins/markitup'
MARKITUP_MEDIA_URL = '/static/'
MARKITUP_AUTO_PREVIEW = True
JQUERY_URL = '/static/js/jquery-1.3.2.min.js'
TAGGING_AUTOCOMPLETE_JS_BASE_URL = "/static"



try:
    from local_settings import *
except ImportError: pass
SESSION_EXPIRE_AT_BROWSER_CLOSE = True

当我在浏览器 URL 上查找图像时 =http://127.0.0.1:8000/static/site_img/logo.gif

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/static/site_img/logo.gif
"/Users/iceman/Documents/gototest/qbank/qbank/static/site_img/logo.gif" does not exist
4

2 回答 2

3

为了回答您的具体问题,我认为您的 STATICFILES_DIRS 密钥丢失了。

这似乎是您的调试配置文件,所以我想您想使用 django 内置开发 Web 服务器来提供您的文件(即:执行“manage.py runserver 8000”)

1.调试=真|假

您必须知道 django 不应该用于在生产中提供文件,并且会拒绝这样做。如果你有“DEBUG=False”,django 默认会拒绝提供静态文件。

您必须使用另一台服务器(大多数人使用 nginx 或在线云服务,例如 cloudfront)

为此,有一个 2 步过程 1. 您需要收集静态文件 2. 设置另一台服务器为它们提供服务

开发时这不方便,所以 django 为您查看 STATICFILES_DIRS,并提供静态文件

2. 收集文件

django 网站由多个应用程序组成(请参阅配置中的“INSTALLED_APPS”键)。它们中的每一个都可以声明静态文件,所有这些文件都位于文件系统的不同目录中。

为了使您的文件得到正确处理,您需要将它们分组在一个本地目录或一个远程服务器中,因此是“./manage.py collectstatic”。

此命令将扫描 STATICFILES_DIRS 中列出的每个目录以及已加载应用程序中的每个“静态”目录,然后使用 STATICFILES_STORAGE 和 STATIC_ROOT 将文件复制到您想要的位置)

3.配置键

# Tells django where your static files are
STATICFILES_DIRS = (
    os.path.join(os.path.dirname(__file__), '../static/generated'),
    os.path.join(os.path.dirname(__file__), '../static/fixed'),
)

# Only for developpement
# Tells to django build in web server where to host your files ("http://localhost/static")
STATIC_URL = '/static/'

# for production, tell django how do {% static %} template tags and "./manage collectstatic" behave (collect files to a single directory, upload them to a CDN, etc)
# 
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'

# Only for production when using default value for STATICFILES_STORAGE
# Tell django where to collect files when you run "./manage.py collectstatic".
# This is only used if you host your static files in the same machine as the rest of your application
STATIC_ROOT = '/var/www/static.mydomain.com/'
于 2013-05-10T07:33:39.327 回答
0

尝试将STATICFILES_FINDERSSTATICFILES_STORAGE(用于部署)添加到您的设置文件中。

这将使您在 Django 中的生活变得更加轻松:您在每个应用程序中放置一个静态文件夹,以供开发时使用,在部署时,您在项目上运行collect_static命令,所有应用程序的所有静态数据都将收集到设置中指定的文件夹STATICFILES_STORAGE

在部署环境中,STATICFILES_STORAGE应该由 Web 服务器提供服务。

于 2013-05-10T08:04:39.277 回答