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

时间:2012-05-05 08:04:33

标签: django

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

3 个答案:

答案 0 :(得分:15)

实质上,您希望django在开发中提供静态文件。准备好投入生产之后,您希望服务器为您执行此操作(它们可以快速完成: - )

这是一个基本设置,一旦你登录服务器,你运行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",
    )
}

答案 1 :(得分:4)

静态文件是应用程序所需的文件,服务器无需修改即可提供,例如自定义JS脚本,图标,小程序等。使用它的最佳方法是将静态文件放在每个文件的“静态”文件夹中app的文件夹。像这样,测试服务器会在那里找到它们,如果你在生产服务器上部署,你只需要运行python manage.py collectstatic将它们全部复制到settings.py

媒体文件是您的应用程序用户上传的文件,例如头像的图片等。

管理员文件是Django管理员使用的静态文件,django测试服务器只会找到它们,但在生产时,您必须复制或链接到此文件夹,以便管理员实际工作。

希望它可以帮助你更好地看待事情......

答案 2 :(得分:1)

我的配置是:

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