简体   繁体   中英

Why won't Django load my static CSS files?

I'm having trouble getting Django to load my CSS for my html template. I'm aware that there are a lot of posts like this, such as here and here , but I'm not sure what else to do here as I've tried several of these types of these solutions to no avail.

Loading up my website using python manage.py runserver returns this log:

[31/Jul/2016 01:58:29] "GET / HTTP/1.1" 200 1703
[31/Jul/2016 01:58:29] "GET /static/css/about.css HTTP/1.1" 404 1766

I'm not sure if the 404 on the end of the second line of the log refers to a 404 error or not.

I have tried adding the static files to my urls.py :

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', TemplateView.as_view(template_name='about.html'))
]

urlpatterns += staticfiles_urlpatterns()

I have tried modifying my settings.py :

STATIC_ROOT = ''

STATIC_URL = '/static/'

STATICFILES_DIR = os.path.dirname(os.path.abspath(__file__)) + "/static/"

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)

And I of course used the proper loading methods in the actual template itself:

{% load staticfiles %}
...
<link rel="stylesheet" href="{% static 'css/about.css' %}">

Here's my file structure:

Personal_Website
|Personal_Website
||settings.py
||urls.py
||etc...
|static
||css
|||about.css

Frankly, I'm not sure what else to do here. I feel like I've tried everything with the settings and still I'm getting that same log.

You should have this to serve static file in project directory...

STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),]

os.path.dirname(os.path.abspath(__file__)) This is pointing to Personal_Website folder where settings all file is there...

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