本质上,您希望在开发中通过 django 提供静态文件。一旦您准备好投入生产,您希望服务器为您执行此操作(它们的构建速度很快:-))
这是一个基本设置,登录服务器后,运行collectstatic命令以获取服务器指向的静态根文件夹中的所有静态文件(请参阅重写规则)
./manage.py collectstatic
设置.py
from os import path
import socket
PROJECT_ROOT = path.dirname(path.abspath(__file__)) #gets directory settings is in
# Dynamic content is saved to here
MEDIA_ROOT = path.join(PROJECT_ROOT,'media')
# if ".webfaction.com" in socket.gethostname():
# MEDIA_URL = 'http://(dev.)yourdomain.com/media/'
# else:
MEDIA_URL = '/media/'
# Static content is saved to here --
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root') # this folder is used to collect static files in production. not used in development
STATIC_URL = "/static/"
STATICFILES_DIRS = (
('', path.join(PROJECT_ROOT,'static')), #store site-specific media here.
)
# 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',
)
设置_部署.py
from settings import *
DEBUG = False
TEMPLATE_DEBUG = DEBUG
MEDIA_URL = "http://yourdomain.com/media/"
urls.py
...other url patterns...
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns() #this serves static files and media files.
#in case media is not served correctly
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
django.conf(lighttpd,这可能是 apache 或 nginx),但我相信 webfaction 有一个应用程序服务可以轻松设置它
$HTTP["host"] =~ "(^|\.)yourdomain\.com$" {
fastcgi.server = (
"/django.fcgi" => (
"main" => (
"socket" => env.HOME + "/project/project.sock",
"check-local" => "disable",
)
),
)
alias.url = (
"/media" => env.HOME + "/project/media",
"/static" => env.HOME + "/project/static-root",
)
url.rewrite-once = (
"^(/media.*)$" => "$1",
"^(/static.*)$" => "$1",
"^/favicon\.ico$" => "/static/img/favicon.png",
"^(/.*)$" => "/django.fcgi$1",
)
}