栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.*)$" => "$1",        "^(/static.*)$" => "$1",        "^/favicon.ico$" => "/static/img/favicon.png",        "^(/.*)$" => "/django.fcgi$1",    )}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/414984.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号