Django 1.5 GET 404静态文件

时间:2013-08-01 22:15:26

标签: python django static http-status-code-404

我需要一些帮助,我一直在寻找没有结果的解决方案。

这是我的设置: settings.py:

STATIC_ROOT = ''

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

STATIC_URL = '/static/'

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

STATICFILES_DIRS = (
    PROJECT_ROOT + '/static/'
)

已安装的应用:

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin', . . .

使用DEBUG = TRUE运行:

August 01, 2013 - 16:59:44
Django version 1.5.1, using settings 'settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[01/Aug/2013 16:59:50] "GET / HTTP/1.1" 200 6161
[01/Aug/2013 16:59:50] "GET /static/media/css/jquery-ui/ui-lightness/jquery-ui-    1.10.3.custom.min.css HTTP/1.1" 404 5904
[01/Aug/2013 16:59:50] "GET /static/media/css/bootstrap/bootstrap.css HTTP/1.1" 404 5904
[01/Aug/2013 16:59:50] "GET /static/media/css/bootstrap/bootstrap-responsive.min.css     HTTP/1.1" 404 5904
[01/Aug/2013 16:59:50] "GET /static/media/css/styles.css HTTP/1.1" 404 5904
[01/Aug/2013 16:59:50] "GET /static/media/js/jquery/jquery-1.9.1.min.js HTTP/1.1" 404 5904
[01/Aug/2013 16:59:50] "GET /static/media/js/bootstrap/bootstrap.min.js HTTP/1.1" 404 5904
[01/Aug/2013 16:59:50] "GET /static/media/js/jquery-ui/jquery-ui-1.10.3.custom.min.js HTTP/1.1" 404 5904
[01/Aug/2013 16:59:50] "GET /static/media/js/messages.js HTTP/1.1" 404 5904
[01/Aug/2013 16:59:50] "GET /static/media/js/validate/jquery.validate.min.js HTTP/1.1" 404 5904
[01/Aug/2013 16:59:50] "GET /static/media/images/FERREMOLQUES2.png HTTP/1.1" 404 5904
[01/Aug/2013 16:59:50] "GET /static/media/js/dynamic-style.js HTTP/1.1" 404 5904

特别提到我在 VIRTUALENV 中运行Django 1.5.1和Python 2.7.5。我不知道这种配置是否导致问题

任何帮助都会受到赞赏

感谢。

编辑:当我关闭VIRTUALENV并安装正确版本的Django和项目的依赖项时,我的项目运行良好,没有任何问题。 。 。静态显示应该

3 个答案:

答案 0 :(得分:8)

您确定STATICFILE_DIRS是否正确?如果您的设置与此类似,则static文件夹应与settings.py处于同一级别。

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) # it means settings.py is in PROJECT_ROOT?
STATICFILES_DIRS = (
    PROJECT_ROOT + '/static/', # <= don't forget a comma here
)

我的正常settings.py有点不同:

ROOT_PATH = path.join(path.dirname(__file__), '..')  # up one level from settings.py
STATICFILES_DIRS = (
    path.abspath(path.join(ROOT_PATH, 'static')), # static is on root level
)

除此之外,您需要django.core.context_processors.static作为上下文处理器:

TEMPLATE_CONTEXT_PROCESSORS = (
    # other context processors....
    'django.core.context_processors.static',
)

并在urls.py中启用urlpattern:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

希望它有所帮助!

答案 1 :(得分:3)

在搜索任何解决方案的数小时和数小时内,我终于发现此问题是一个错误:

https://bugzilla.redhat.com/show_bug.cgi?id=962223

我不确定这个错误是由Django还是Python,我的Django版本是1.5.1而Python是2.7.5。我需要在之前的django和python版本中证明是否存在bug。

我的setting.py位于DEBUG=False,当我将其更改为True时问题已经消失,现在正处于开发状态,我并不担心,但是当我的项目达到生产时,我等待补丁。

再次感谢。

答案 2 :(得分:1)

这个问题正在发生,因为在NON DEBUG模式下,静态文件处理程序是WSGI,如下面的代码行,它不处理settings.STATIC_ROOT路径 -

文件位置 - django.contrib.staticfiles.management.commands.runserver

def get_handler(self, *args, **options):
    """
    Returns the static files serving handler wrapping the default handler,
    if static files should be served. Otherwise just returns the default
    handler.

    """
    handler = super(Command, self).get_handler(*args, **options)
    use_static_handler = options.get('use_static_handler', True)
    insecure_serving = options.get('insecure_serving', False)
    if use_static_handler and (settings.DEBUG or insecure_serving):
        return StaticFilesHandler(handler)
    return handler

为了解决这个问题,我添加了一个其他检查,如下面的代码所示,它工作正常。

  def get_handler(self, *args, **options):
        """
        Returns the static files serving handler wrapping the default handler,
        if static files should be served. Otherwise just returns the default
        handler.

        """
        handler = super(Command, self).get_handler(*args, **options)
        use_static_handler = options.get('use_static_handler', True)
        insecure_serving = options.get('insecure_serving', False)
        if use_static_handler and (settings.DEBUG or insecure_serving):
            return StaticFilesHandler(handler)
        elif use_static_handler: 
            return StaticFilesHandler(handler)
        return handler

谢谢,
拉夫