我是Django(1.4)的新手,我很难理解静态文件,媒体文件和管理文件的原理。该项目的结构从一个教程到另一个教程都是不同的,对于Webfaction(我将在其中托管应用程序)也是如此。我想知道什么是组织它的最佳方式,并且在将它部署到Webfaction时花费最少的精力和编辑,静态媒体和管理文件的意义是什么?先感谢你
本质上,你想在开发中使用django提供静态文件。准备好投入生产后,你希望服务器为你执行此操作(它们的构建旨在快速完成它:-))
这是一个基本设置,登录服务器后,运行collectstatic命令以获取服务器指向的static-root文件夹中的所有静态文件(请参阅重写规则)。
./manage.py collectstatic
settings.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', )
settings_deployment.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", ) }