混乱在Django管理员,静态和媒体文件 [英] Confusion in Django admin, static and media files

查看:87
本文介绍了混乱在Django管理员,静态和媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Django(1.4)比较新,我很难理解静态,媒体和管理文件背后的理念。项目的结构不同于一个教程到另一个教程,Webfaction也是同样的事情(我将在其中托管我的应用程序)。我想知道什么是组织它的最佳方式,最少的痛苦和编辑,同时部署到Webfaction,静态媒体,adn管理员文件是什么?
提前谢谢你

解决方案

本质上你想在django中开发静态文件。一旦你准备开始生产,你希望服务器为你做这个(他们正在建立快速: - ))



这是一个基本的设置,一旦您登录服务器,您运行collectstatic命令来获取静态根文件夹中的所有静态文件(服务器指向的内容)(请参阅重写规则)

  ./ manage.py collectstatic 

settings.py

 从os导入路径
import socket

PROJECT_ROOT = path.dirname(path.abspath(__ file__)) #gets目录设置在

#动态内容保存到此
MEDIA_ROOT = path.join(PROJECT_ROOT,'media')
#如果.webfaction.com在socket.gethostname():
#MEDIA_URL ='http://(dev。)yourdomain.com/media/'
#else:
MEDIA_URL ='/ media /'

#静态内容保存到这里 -
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root') #此文件夹用于收集生产中的静态文件。在开发中不使用
STATIC_URL =/ static /
STATICFILES_DIRS =(
('',path.join(PROJECT_ROOT,'static')),这里是#store站点特定的媒体。


#知道如何在
#各个位置找到静态文件的finder类的列表。
STATICFILES_FINDERS =(
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles .finders.DefaultStorageFinder',

settings_deployment.py

 从设置导入* 

DEBUG = False
TEMPLATE_DEBUG = DEBUG
MEDIA_URL =http:// yourdomain.com/media/

urls.py

  ...其他网址模式... 

如果settings.DEBUG:
urlpatterns + = staticfiles_urlpatterns()#this提供静态文件和媒体文件。
#in case media was not served correct
urlpatterns + = patterns('',
url(r'^ media /(?P< path>。*)$','django。 view.static.serve',{
'document_root':settings.MEDIA_ROOT,
}),

django.conf(lighttpd,这可能是apache或nginx),但我相信webfaction有一个应用程序服务来轻松设置这个

  $ HTTP [host] =〜(^ | \。)yourdomain\.com ${
fastcgi.server =(

main=>(
socket=> env.HOME +/project/project.sock,
check-本地=>禁用,

),

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,

}


I'm relatively new to Django (1.4) and I'm having a hard time understanding the philosophy behind static, media, and admin files. The structure of the project is different from one tutorial to another, and same thing for Webfaction(where I'll be hosting my application). I would like to know what is the optimal way to organize it and with least pain and editing while deploying it to Webfaction, what is the point of static media, adn admin files? Thank you in advance

解决方案

In essence you want to serve static files by django in development. Once you're ready to go into production you want the server to do this for you (they are build to do it fast :-))

Here's a basic setup, once you login the server you run the collectstatic command to get all the staticfiles in the static-root folder, which your server points to (see the rewrite rules)

./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, this could be apache or nginx) but I believe webfaction has an app service to set this up easily

$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",
    )
}

这篇关于混乱在Django管理员,静态和媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆