Django 管理、静态和媒体文件中的混淆 [英] Confusion in Django admin, static and media files

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

问题描述

我对 Django (1.4) 比较陌生,我很难理解静态、媒体和管理文件背后的哲学.该项目的结构从一个教程到另一个教程都不同,对于 Webfaction(我将在其中托管我的应用程序)也是如此.我想知道什么是组织它的最佳方式,在将它部署到 Webfaction 时,痛苦和编辑最少,静态媒体,adn 管理文件有什么意义?提前谢谢您

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

推荐答案

本质上,您希望在开发中通过 django 提供静态文件.一旦您准备好投入生产,您就希望服务器为您执行此操作(它们的构建是为了快速完成 :-))

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 :-))

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

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

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

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 有一个应用程序服务可以轻松设置

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天全站免登陆