简体   繁体   English

我的 django 项目 static 文件无法使用选项 debug=false 加载

[英]my django project static files can not be load with option debug=false

It is very strange that my django website (setup on a linux server with django 1.3) can be visit correctly with DEBUG = True.很奇怪,我的 django 网站(在 linux 服务器上设置 django 1.3)可以正确访问 DEBUG = True。 But when I changed the option to DEBUG = False the static content won't load (css files and images can not be found)但是当我将选项更改为 DEBUG = False 时,static 内容将无法加载(找不到 css 文件和图像)

Here's related options i got in my setting.py:这是我在setting.py中得到的相关选项:

DEBUG = False
STATIC_ROOT = ''
ADMIN_MEDIA_PREFIX = '/static/admin/'
STATICFILES_DIRS = ("/home/ubuntu/ls_website/static/",)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

Anybody have any idea?有人知道吗? If the version is too low, I would wait a while for 1.5 release.如果版本太低,我会等待 1.5 发布。

I found when debug=False, django won't load static files automatically.我发现当 debug=False 时,django 不会自动加载 static 文件。 Options are set static path in urls.py or serve static files with apache.选项在 urls.py 中设置 static 路径或使用 apache 提供 static 文件。 People said set static path in urls.py is slower than serve static files with apache.人们说在 urls.py 中设置 static 路径比使用 apache 提供 static 文件要慢。 Don't know why though...虽然不知道为什么...

Staticfiles doesn't do anything when DEBUG=False.当 DEBUG=False 时,静态文件不做任何事情。 You need to serve those files with apache.您需要使用 apache 提供这些文件。 Staticfiles has the ability to collect the files to one spot for you to make it easy, then you use some apache magic (assuming here since you didn't specify) to have apache intercept those requests and serve the static files for you. Staticfiles 能够将文件收集到一个位置以便您轻松完成,然后您使用一些 apache 魔法(假设这里因为您没有指定)让 apache 拦截这些请求并为您提供 ZA81259CEF8E959C6297ZDF1D4 文件。

    Alias /robots.txt  /home/username/Python/project/site_media/static/robots.txt
    Alias /favicon.ico /home/username/Python/project/site_media/static/favicon.ico
    Alias /static/     /home/username/Python/project/site_media/static/

I don't remember if it is buildstatic, build_static, collectstatic or collect_static to copy those files from your development stop to your deployment spot, but these variables control how staticfiles does its magic我不记得将这些文件从开发站复制到部署点是 buildstatic、build_static、collectstatic 还是 collect_static,但是这些变量控制着 staticfiles 如何发挥它的魔力

# Absolute path to the directory that holds static files like app media.
# Example: "/home/media/media.lawrence.com/apps/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static")

# URL that handles the static files like app media.
# Example: "http://media.lawrence.com"
STATIC_URL = "/static/"

# Additional directories which hold static files
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, "static"),
    os.path.join(PROJECT_ROOT, "media"),
]

This assumes your static files are in the static folder of your project, and you want to serve them from the site_media folder.这假设您的 static 文件位于项目的static文件夹中,并且您希望从site_media文件夹中提供它们。

I also move between Windows and Linux for development.我也在 Windows 和 Linux 之间移动以进行开发。 To get around the problem of absolute paths in settings.py, I use this function:为了解决settings.py中绝对路径的问题,我使用了这个function:

def map_path(directory_name):
    return os.path.join(os.path.dirname(__file__),
        '../' + directory_name).replace('\\', '/')

Which you can implement in this fashion:您可以以这种方式实现:

STATICFILES_DIRS = (
    map_path('static'),
)

This function is tailored for Django 1.4.x.此 function 专为 Django 1.4.x 量身定制。 You'll need to modify it slightly for Django 1.3.x.您需要为 Django 1.3.x 稍微修改它。 Hope that helps you out.希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM