Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

Confusion in Django admin, static and media files

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...