15

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

4

3 回答 3

15

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

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

./manage.py collectstatic

设置.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/"

网址.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",
    )
}
于 2012-05-05T08:18:30.203 回答
4

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

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

管理文件是 Django 管理员使用的静态文件,django 测试服务器只会找到它们,但在生产环境中,您必须复制或链接到此文件夹才能让管理员真正工作。

希望它可以帮助您更好地了解事物...

于 2012-05-05T08:22:34.247 回答
1

我的配置是:

1.设置.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
于 2013-10-01T15:18:08.533 回答