简体   繁体   中英

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:

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:

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 . 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. . . statics are shown as it should

Are you sure your STATICFILE_DIRS is correct? If your settings is like at the moment, the static folder is supposed to be in same level as 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:

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:

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

And enable the urlpattern in urls.py :

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

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. I would need to proof in previous django and python version to see if bug is present.

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.

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 -

File Location - 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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