Django — Can't get static CSS files to load

后端 未结 20 1690
借酒劲吻你
借酒劲吻你 2020-11-29 23:45

I\'m running Django\'s development server (runserver) on my local machine (Mac OS X) and cannot get the CSS files to load.

Here are the relevant entries

相关标签:
20条回答
  • 2020-11-30 00:11

    added

    PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
    STATICFILES_DIRS = ( os.path.join(PROJECT_ROOT, "static"), )
    

    and removed STATIC_ROOT from settings.py, It worked for me

    0 讨论(0)
  • 2020-11-30 00:12

    Go to your HTML page load static by

    {% load static %}
    

    Now only mistake I've made was this

    My code:

    <img src="**{% static** "images/index.jpeg" %}" alt="My image">
    

    Updated:

    <img src=**"{% static 'images/index.jpeg' %}' alt="My image"**>
    

    You get it right

    0 讨论(0)
  • 2020-11-30 00:13

    Read this carefully: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/

    Is django.contrib.staticfiles in your INSTALLED_APPS in settings.py?

    Is DEBUG=False? If so, you need to call runserver with the --insecure parameter:

    python manage.py runserver --insecure
    

    collectstatic has no bearing on serving files via the development server. It is for collecting the static files in one location STATIC_ROOT for your web server to find them. In fact, running collectstatic with your STATIC_ROOT set to a path in STATICFILES_DIRS is a bad idea. You should double-check to make sure your CSS files even exist now.

    0 讨论(0)
  • 2020-11-30 00:15

    DEBUG = True in my local settings did it for me.

    0 讨论(0)
  • 2020-11-30 00:17

    Are these missing from your settings.py? I am pasting one of my project's settings:

    TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.contrib.messages.context_processors.messages")
    

    Also, this is what I have in my urls.py:

    urlpatterns += patterns('', (
            r'^static/(?P<path>.*)$',
            'django.views.static.serve',
            {'document_root': 'static'}
    ))
    
    0 讨论(0)
  • 2020-11-30 00:17

    You can just set STATIC_ROOT depending on whether you are running on your localhost or on your server. To identify that, refer to this post.

    And you can rewrite you STATIC_ROOT configuration as:

    import sys
    
    if 'runserver' in sys.argv:
        STATIC_ROOT = ''
    else:
        STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    STATIC_URL = '/static/'
    
    0 讨论(0)
提交回复
热议问题