Static media fails to load in Django

2019-08-06 04:24发布

My settings.py contains the following configuration parameters.

STATIC_ROOT = ''

STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    'C:/Users/ABC/Desktop/DBMS/DjangoProject/tvshows',
)

My project's CSS file is located at C:/Users/ABC/Desktop/DBMS/DjangoProject/tvshows/static/default.css.

I have a mock HTML file that should pull in the CSS content, but the URL is a 404.

<link rel="stylesheet" href="{{ STATIC_URL }}static/default.css" />

What am I doing wrong?

2条回答
Emotional °昔
2楼-- · 2019-08-06 05:12

<link rel="stylesheet" href="{{ STATIC_URL }}default.css" />

You need to also edit your urls:

查看更多
We Are One
3楼-- · 2019-08-06 05:23

Things to check:

  1. DEBUG = True in settings.py

  2. urls

    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    
    # ... the rest of your URLconf goes here ...
    
    urlpatterns += staticfiles_urlpatterns()
    
  3. use context processor or load static if {{ STATIC_URL }} isn't working

    If {{ STATIC_URL }} isn't working in your template, you're probably not using RequestContext when rendering the template.

    As a brief refresher, context processors add variables into the contexts of every template. However, context processors require that you use RequestContext when rendering templates. This happens automatically if you're using a generic view, but in views written by hand you'll need to explicitally use RequestContext To see how that works, and to read more details, check out Subclassing Context: RequestContext.

查看更多
登录 后发表回答