Djangopipe理员,静态和媒体文件混乱
我对Django(1.4)比较新,我很难理解静态,媒体和pipe理文件背后的哲学。 该项目的结构不同,从一个教程到另一个,同样的事情Webfaction(我将托pipe我的应用程序)。 我想知道什么是组织它的最佳方式,在将其部署到Webfaction时最less的痛苦和编辑是什么,静态媒体和pipe理员文件的意义何在? 先谢谢你
本质上你想在开发中由django提供静态文件。 一旦你准备投入生产,你希望服务器为你做这件事(他们正在快速构build:-))
这里有一个基本的设置,一旦你login服务器,你运行collectstatic命令来获取你的服务器指向的静态根文件夹中的所有静态文件(参见重写规则)
./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", ) }
静态文件是您的应用程序所需要的文件,服务器可以在不做任何修改的情况下提供服务,如自定义JS脚本,图标,小应用程序等。使用它的最佳方式是将静态文件放在每个应用文件夹的“静态”文件夹中。 像这样,testing服务器会在那里find它们,如果你在生产服务器上部署,你只需要运行python manage.py collectstatic
把它们全部复制到settings.py中定义的根静态文件夹中
媒体文件是由您的应用程序的用户上传的,如头像的图片等
pipe理文件是Djangopipe理员使用的静态文件,djangotesting服务器只能find它们,但是在生产中,您必须复制或链接到该文件夹才能使pipe理员实际工作。
希望它能帮助你更好地看到事物
我的configuration是:
1.settings.py
# Absolute filesystem path to the directory that will hold user-uploaded files. # Example: "/var/www/example.com/media/" MEDIA_ROOT='/media/' # URL that handles the media served from MEDIA_ROOT. Make sure to use a # trailing slash. # Examples: "http://example.com/media/", "http://media.example.com/" MEDIA_URL = '/media/' # Absolute path to the directory static files should be collected to. # Don't put anything in this directory yourself; store your static files # in apps' "static/" subdirectories and in STATICFILES_DIRS. # Example: "/var/www/example.com/static/" STATIC_ROOT = '/static/' # URL prefix for static files. # Example: "http://example.com/static/", "http://static.example.com/" STATIC_URL = '/static/' # Additional locations of static files STATICFILES_DIRS = ( '/'.join(__file__.split(os.sep)[0:-2]+['static']), # Put strings here, like "/home/html/static" or "C:/www/django/static". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. ) # 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', )
2.urls.py
from django.conf import settings if settings.DEBUG: from django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns += staticfiles_urlpatterns()
和我的网站目录是这样的:
root │ manage.py │ ├─media ├─my_django_py3 │ settings.py │ urls.py │ views.py │ wsgi.py │ __init__.py │ ├─static │ 9gq05.jpg │ ajax.js │ favicon.gif │ ├─templates └─utils