Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django loading static files
#1
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?
Reply
#2
Html files have a <head> and a <body>.  If you put the stylesheet link in the head, does it work?
Reply
#3
(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?
Reply
#4
not worked with django, but shouldn't it be STATIC_ROOT = os.path.join(BASE_DIR, 'website2', 'static')?
not sure about STATIC_URL
Reply
#5
(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)
Reply
#6
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'
Reply
#7
(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)
Reply
#8
(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/ho...tic-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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [Solved] Private static files SpongeB0B 1 1,030 Jul-16-2022, 05:36 AM
Last Post: SpongeB0B
  Flask, Display a picture outisde the static SpongeB0B 6 15,251 Aug-29-2020, 05:15 PM
Last Post: nilamo
  SHow image in a static link of Django pycada 3 2,514 Mar-04-2020, 01:50 AM
Last Post: pycada
  Django problem with files menator01 1 1,914 Jan-05-2020, 04:20 PM
Last Post: menator01
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,245 Jun-30-2019, 12:21 AM
Last Post: scidam
  django herf not loading right LK1097 0 2,061 May-31-2018, 06:59 AM
Last Post: LK1097
  first django site-ms word/pdf files jon0852 1 4,345 Nov-19-2017, 08:39 PM
Last Post: homayoon_hashemi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020