Django管理,静态和媒体文件中的混乱

Django管理,静态和媒体文件中的混乱,第1张

Django管理,静态和媒体文件中的混乱

本质上,你想在开发中使用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 = FalseTEMPLATE_DEBUG = DEBUGMEDIA_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.*)$" => "",        "^(/static.*)$" => "",        "^/favicon.ico$" => "/static/img/favicon.png",        "^(/.*)$" => "/django.fcgi",    )}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5036881.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存