Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

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?
buran wrote Oct-11-2017, 03:27 PM:
Please, don't post images, but code in code tags.
Read What to include in a post and What NOT to include in a post
Quote
#2
Html files have a <head> and a <body>.  If you put the stylesheet link in the head, does it work?
It's dangerous to go alone!  Take this: https://docs.python.org/3/library/index.html
Kevin Flynn is my spirit animal.
Quote
#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?
Quote
#4
not worked with django, but shouldn't it be STATIC_ROOT = os.path.join(BASE_DIR, 'website2', 'static')?
not sure about STATIC_URL
Quote
#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)
Quote
#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'
Quote
#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)
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  first django site-ms word/pdf files jon0852 0 35 Oct-17-2017, 10:19 PM
Last Post: jon0852

Forum Jump:


Users browsing this thread: 1 Guest(s)