hasherr hasherr - 4 months ago 13
HTML Question

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.

Answer

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...

Comments