Python Django中的静态内容

Python Django中的静态内容,python,django,Python,Django,我正在尝试为我的Django加载静态内容,我已经升级到Django 1.4 该项目已成功部署,但我无法找到该项目的图像和所有静态内容 请查找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_

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

请查找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

为了解决您的具体问题,我想您的STATICFILES\u DIRS密钥丢失了

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

1。调试=真|假

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

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

为此,有两个步骤 1.您需要收集静态文件 2.设置另一台服务器为其服务

当开发这个工具不方便时,django会为您查看静态文件,并提供静态文件

2。收集文件

# 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/'
django网站由多个应用程序组成(请参阅配置中的“已安装的应用程序”键)。 它们中的每一个都可以声明静态文件,所有这些文件都位于文件系统中的不同目录中

为了正确地提供文件,您需要将它们分组到单个本地目录或单个远程服务器中,因此使用“/manage.py collectstatic”

此命令将扫描STATICFILES\u DIRS中列出的每个目录以及加载的应用程序中的每个“静态”目录,然后使用STATICFILES\u存储和static\u 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/'

为了解决您的具体问题,我想您的STATICFILES\u DIRS密钥丢失了

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

1。调试=真|假

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

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

为此,有两个步骤 1.您需要收集静态文件 2.设置另一台服务器为其服务

当开发这个工具不方便时,django会为您查看静态文件,并提供静态文件

2。收集文件

# 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/'
django网站由多个应用程序组成(请参阅配置中的“已安装的应用程序”键)。 它们中的每一个都可以声明静态文件,所有这些文件都位于文件系统中的不同目录中

为了正确地提供文件,您需要将它们分组到单个本地目录或单个远程服务器中,因此使用“/manage.py collectstatic”

此命令将扫描STATICFILES\u DIRS中列出的每个目录以及加载的应用程序中的每个“静态”目录,然后使用STATICFILES\u存储和static\u 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/'
尝试将和(用于部署)添加到设置文件中

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

在部署环境中,web服务器应为
静态文件存储提供服务。

尝试将和(用于部署)添加到设置文件中

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


在部署环境中,
STATICFILES\u存储
应由web服务器提供服务。

您使用什么作为web服务器?Django内置的?Nginx+gunicorn/uwsgi?Apache+modwsgi?您似乎也没有设置值。我已取消了
静态\u根目录的注释。它仍然是相同的。您使用什么作为Web服务器?Django内置的?Nginx+gunicorn/uwsgi?Apache+modwsgi?您似乎也没有设置值,我已经取消了
静态根的注释,它仍然是一样的