简体   繁体   English

Django Static Files结果为404

[英]Django Static Files results in 404

Ive checked over quite a few of the other threads on being unable to serve static content using the static file app within Django but as yet have yet to find a solution. 我已经检查了很多其他线程,因为它们无法使用Django中的静态文件应用程序提供静态内容,但尚未找到解决方案。

settings.py settings.py

STATIC_ROOT = '/opt/django/webtools/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    "/home/html/static",
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

Template 模板

relevant line.... 相关行...

<img src="{{ STATIC_URL }}macmonster/img/macmonster-logo-blue.png" >

Logs 日志

From the logs it looks like the path is correct, but alas it is still resulting in a 404.. 从日志看来,路径是正确的,但可惜它仍然会产生404。

[10/Feb/2013 16:19:50] "GET /static/macmonster/img/macmonster-logo-blue.png HTTP/1.1" 404 1817
[10/Feb/2013 16:19:51] "GET /static/macmonster/img/macmonster-logo-blue.png HTTP/1.1" 404 1817

For local serving of static files, if you haven't set up any form of collecting of staticfiles and if you're running Django 1.3+, I believe this is the way your settings.py should look like when refering to static files 对于本地提供的静态文件, 如果您尚未设置任何形式的静态文件收集,并且运行的是Django 1.3+,我相信这是您引用静态文件时settings.py外观

# 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: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # 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.
 '/Users/cupcake/Documents/Workspaces/myDjangoProject/someOtherFolderPerhapsIfYouWant/static',
)

# 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',
)

Notice that I've left out the STATIC_ROOT here. 请注意,我在这里省略了STATIC_ROOT This is because I don't have the need for static files collection "just yet". 这是因为“现在”我不需要静态文件收集。

The collecting of static files is to allieviate(spelling) the problems with serving multiple diffrent staticfiles folders, so they merged the staticfiles app that was used normally for helping with this problem. 收集静态文件是为了缓解(拼写)为多个不同的staticfiles文件夹提供服务时遇到的问题,因此他们合并了通常用于解决此问题的staticfiles应用程序。 What this does, and it's described in the docs, is to take all the static files from all your apps and put them into one (1) folder for easier serving when putting your app into production. 它所做的工作(在文档中进行了描述)是从所有应用程序中提取所有静态文件,并将它们放在一(1)个文件夹中,以便在将应用程序投入生产时更容易提供。

So you're problem is that you've "missed" this step and that's why you're getting a 404 when trying to access them. 因此,您的问题是您已“错过”了这一步,这就是为什么尝试访问它们时得到404的原因。 Therefore you need to use a absolute path to your static files ie. 因此,您需要使用静态文件的绝对路径,即。 on a mac or unix system it should look something like this: 在Mac或UNIX系统上,它应该看起来像这样:

'/Users/cupcake/Documents/Workspaces/myDjangoProject/someOtherFolderPerhapsIfYouWant/static',

Also, you could simplify and "fix" the need of having a hardcoded path like that which I used for illustration and do like this 另外,您可以简化和“修复”具有像我用于说明的硬编码路径的需求,并这样做

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

STATICFILES_DIRS = (
    PROJECT_ROOT + '/static/'
)

This would fix the portability problem as well. 这也将解决可移植性问题。 A good Stackoverflow post about that is found here 可以在这里找到有关该问题的出色Stackoverflow帖子

I hope I made it a bit clearer, otherwise please correct me if I'm wrong ^_^! 我希望我把它弄清楚一些,否则,如果我错了,请纠正我^ _ ^!

For collecting and how to manage static files in the newer versions of Django read this link The staticfiles app 有关收集和如何在较新版本的Django中管理静态文件的信息,请阅读此链接staticfiles应用

Change 更改

STATIC_URL = '/static/'

set

STATIC_URL = 'http://yourdomain.com/static/'

it's unbelievable but, after 1 hour searching it solution solve my problem with static files and remove STATIC_ROOT from STATICFILES_DIRS . 这是令人难以置信的,但1小时后寻找问题解决方案解决我的问题与静态文件,并删除STATIC_ROOTSTATICFILES_DIRS STATICFILES_DIRS is just for collecting all the static in modules and store it in STATIC_ROOT . STATICFILES_DIRS仅用于收集模块中的所有静态并将其存储在STATIC_ROOT

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

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