简体   繁体   English

Django 1.5 GET 404静态文件

[英]Django 1.5 GET 404 on static files

I need a little help with this, I've been searching for a solution with no results. 我需要一些帮助,我一直在寻找没有结果的解决方案。

This are my settings: settings.py: 这是我的设置: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: 已安装的应用:

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

Running with DEBUG = TRUE: 使用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

As a special mention I'm running Django 1.5.1 and Python 2.7.5 in a VIRTUALENV . 特别值得一提的是,我在VIRTUALENV中运行Django 1.5.1和Python 2.7.5。 I do not know if this configuration is causing the problem 我不知道这种配置是否导致问题

Any help would be appreciate 任何帮助将是欣赏

Thanks. 谢谢。

EDIT: When I off VIRTUALENV and install proper version of Django and the project's dependencies, My project works well, without any issue. 编辑:当我关闭VIRTUALENV并安装适当版本的Django和项目的依赖项时,我的项目运行良好,没有任何问题。 . . statics are shown as it should 静态显示应该如此

Are you sure your STATICFILE_DIRS is correct? 你确定你的STATICFILE_DIRS是正确的吗? If your settings is like at the moment, the static folder is supposed to be in same level as settings.py . 如果您的设置与目前相同,则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
)

My normal settings.py is a bit different: 我的普通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
)

Apart from that, you need django.core.context_processors.static as context processors: 除此之外,您需要django.core.context_processors.static作为上下文处理器:

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

And enable the urlpattern in urls.py : 并在urls.py启用urlpattern:

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

Hope it helps! 希望能帮助到你!

For hours and hours of searching for any solution, finally I found that this problem is a bug: 搜索任何解决方案的数小时和数小时,最后我发现这个问题是一个错误:

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

I'm not sure if this bug is by Django or Python, My Django version is 1.5.1 and Python is 2.7.5. 我不确定这个错误是由Django还是Python,我的Django版本是1.5.1而Python是2.7.5。 I would need to proof in previous django and python version to see if bug is present. 我需要在之前的django和python版本中进行证明,看看是否存在bug。

My setting.py was in DEBUG=False when I change it to True the problem has gone, right now in development, I'm not worried about that, but I wait for a patch when my project reach production. 当我将其更改为True时,我的setting.py处于DEBUG=False ,问题已经消失,现在正处于开发阶段,我并不担心,但是当我的项目达到生产时,我等待补丁。

Thanks again. 再次感谢。

This issue is happening because in NON DEBUG mode static files handler is WSGI as per below code lines, which does not handle settings.STATIC_ROOT path - 这个问题正在发生,因为在NON DEBUG模式下,静态文件处理程序是WSGI,如下面的代码行,它不处理settings.STATIC_ROOT路径 -

File Location - django.contrib.staticfiles.management.commands.runserver 文件位置 - 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

To fix this, I added one else check as shown in below code and it works fine. 为了解决这个问题,我添加了一个其他检查,如下面的代码所示,它工作正常。

  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

Thanks, 谢谢,
Gaurav 拉夫

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

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