Forums

Django and static files.

My static files are not being served to my website. I followed the guide here and set everything up accordingly but they are not working. I have a static folder within my app that stores all the static files for the website and it collects them fine when i run collectstatic and puts them in the project static folder but they do not load at all. I tried clearing my cache in my browser too and checked on a different computer to make sure they weren't loading also.

# settings.py

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

under the web tab 'Static files:'
url = /static/
directory = /home/uberfade/myproject/static

Any help is appreciated.

I fixed my initial problem with static files not loading at all. I had to put the extra directory in the paths that Django recommends for namespacing on development you don't have to do that I guess. I am still having trouble loading one of the images though. It is a background image being loaded from my css file. it uses a relative path.

background-image: url(../img/home_bg.jpg);

Is there some other way I need to point to this file?

Is this just because your are accessing this from different places? (ie. if you are accessing ../img/home_bg.jpg from /home vs /another/url/, then the relative path will be for different urls?)

I'm not sure what you mean. The relative path should be the same still. The static files are setup fairly standard according to django/pythonanywhere. The file layout:

projectname
|
|->static
|->projectname
|   |
|   |--settings.py
|   |--urls.py
|->appname
|  |
|  |->static
|  |  |
|  |  |->appname
|  |  |  |
|  |  |  |->css
|  |  |  |  |--base.css
|  |  |  |->img
|  |  |  |  |--home_bg.jpg
|  |  |  |->js
|  |  |  |  |--base.js
|  |->templates
|  |  |   
|  |  |->appname
|  |  |  |--home.html
|  |  |  |--about.html
|  |  |  |--contact.html
|  |--views.py
|  |--models.py
|  |--admin.py
|  |--forms.py
|--manage.py

The file I am referring to is base.css that has the line:

# base.css

background-image: url(../img/home_bg.jpg);

The only html page that actually uses that is the home.html so it shouldn't be wrong.

Hmm. Maybe double check that the absolute path version works first?

I don't see any static file settings on the "Web" tab for your website, apart from some for a Let's Encrypt certificate -- are you still having the problem?