Python Forum
Django loading static files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Django loading static files (/thread-5570.html)



Django loading static files - Dutchpy - Oct-11-2017

I'm trying to load some CSS files but when I load the page it just shows the HTML.

Here is what my HTML file looks like:
https://imgur.com/a/48nkT

Did I do something wrong?


RE: Django loading static files - nilamo - Oct-11-2017

Html files have a <head> and a <body>.  If you put the stylesheet link in the head, does it work?


RE: Django loading static files - Dutchpy - Oct-12-2017

(Oct-11-2017, 09:51 PM)nilamo Wrote: Html files have a <head> and a <body>.  If you put the stylesheet link in the head, does it work?

yeah I thought about that later on, it didn't make any difference.
Maybe there is something wrong in my settings?

I currently have
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")

Do you think these are okay and are there any other settings I should be concerned about?


RE: Django loading static files - buran - Oct-12-2017

not worked with django, but shouldn't it be STATIC_ROOT = os.path.join(BASE_DIR, 'website2', 'static')?
not sure about STATIC_URL


RE: Django loading static files - Dutchpy - Oct-12-2017

(Oct-12-2017, 09:05 PM)buran Wrote: not worked with django, but shouldn't it be STATIC_ROOT = os.path.join(BASE_DIR, 'website2', 'static')?
not sure about STATIC_URL

I just tried it but it doesn't make a difference unfortunately.

I'm very inexperienced with programming in general, could it have to do with the way the folders are structured?
the templates folder with my html files is on an equal level with the website 2 folder. the static folder that the html template is supposed to interact with is one "level lower" in the static map that is inside the website 2 folder. if that makes sense. (my initial screenshot of this topic may clarify what I'm trying to say)


RE: Django loading static files - hbknjr - Oct-13-2017

you should have pasted the code here and not a screenshot link.

Django gives a real helpful traceback of error when debug is set to True. You can find the problem there.

By the way, in development Django serves the static files by collecting all of them(from different apps) in a single folder static. So your path needs to be according to that.

It should be href='{% static "test2.css" %}'.
for folder structure:

'static'/
    'test2.css'
It is a common practice to put the static files in a folder with the same name of the app(which you have done). For that, your folder structure should be.

'static'/
    'website2'/
        'test2.css'



RE: Django loading static files - Dutchpy - Oct-13-2017

(Oct-13-2017, 07:54 AM)hbknjr Wrote: you should have pasted the code here and not a screenshot link.

Django gives a real helpful traceback of error when debug is set to True. You can find the problem there.

By the way, in development Django serves the static files by collecting all of them(from different apps) in a single folder static. So your path needs to be according to that.

It should be href='{% static "test2.css" %}'.
for folder structure:

'static'/
    'test2.css'
It is a common practice to put the static files in a folder with the same name of the app(which you have done). For that, your folder structure should be.

'static'/
    'website2'/
        'test2.css'

I thought it was useful to include a screenshot so you could see the way my folders are structured, but I'll keep that in mind next time!
I followed your suggestion but it doesn't seem to work yet. when I go to the page on my local server I still only get HTML without CSS. The terminal tells me:

[13/Oct/2017 09:50:43] "GET /hello/ HTTP/1.1" 200 847
[13/Oct/2017 09:50:43] "GET /static/test2.css HTTP/1.1" 404 1640

is that the error message you reffered to?

(debug is set to true in the settings.py file)


RE: Django loading static files - hbknjr - Oct-13-2017

(Oct-13-2017, 09:57 AM)Dutchpy Wrote: is that the error message you referred to?


Sorry my bad, Django doesn't give an error for static files its only templates.

You are getting 404 not found error.
refer to: https://docs.djangoproject.com/en/2.0/howto/static-files/

make sure you have these settings:

PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)

# Static files (CSS, JavaScript, Images)
STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

STATICFILES_DIRS = [                # For static files not particular to any app.
    os.path.join(PROJECT_DIR, 'static'),
]

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'                # eg 127.0.0.1/static/
If still, it doesn't work try:
python manage.py collectstatic

And check your STATIC_ROOT. Your file should exist there and be served too.