Django静态文件导致404 [英] Django Static Files results in 404

查看:87
本文介绍了Django静态文件导致404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了很多其他线程,因为无法使用Django内的静态文件应用程序提供静态内容,但尚未找到解决方案。

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.

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

模板

相关行....

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

日志

从日志中看起来路径是正确的,但唉,它仍然导致了一个404 ..

From the logs it looks like the path is correct, but alas it is still resulting in a 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


推荐答案

对于静态文件的本地服务,如果您没有设置任何形式的静态文件集合,如果您正在运行Django 1.3+,我相信这是您的 settings.py 应该像引用静态文件时一样。

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

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

请注意,我已经省略了 STATIC_ROOT 这里。
这是因为我不需要静态文件集刚才。

Notice that I've left out the STATIC_ROOT here. This is because I don't have the need for static files collection "just yet".

收集静态文件是为了填充(拼写)服务多个不同的静态文件夹文件夹的问题,所以他们合并了通常用于帮助这个问题的 staticfiles 应用程序。
这是什么,它在文档中描述的是从所有应用程序中获取所有静态文件,并将它们放入一(1)个文件夹,以便将应用投入生产时更容易服务。

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. 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.

所以你的问题是你错过了这一步,这就是为什么你尝试访问404时。
因此,您需要使用静态文件的绝对路径即。在mac或unix系统上,应该看起来像这样:

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. 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:

'/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/'
)

这也将修复可移植性问题。有一个很好的Stackoverflow帖子被发现这里

This would fix the portability problem as well. A good Stackoverflow post about that is found here

我希望我更清楚一点,否则如果我错了,请更正我^ _ ^!

I hope I made it a bit clearer, otherwise please correct me if I'm wrong ^_^!

为了收集和如何管理较新版本的Django中的静态文件,请阅读此链接
staticfiles应用程序

For collecting and how to manage static files in the newer versions of Django read this link The staticfiles app

这篇关于Django静态文件导致404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆