Forums

Help with serving static files to a subpage

Hello,

I've followed the tutorial for static files (which is great btw) and everything works for the index.html page. However, when trying to go to a second page, it throws an error which fits one of the issues described in this other link: https://help.pythonanywhere.com/pages/DebuggingStaticFiles/
The path to the file and the path in the URL don't quite match (eg, there's an extra level of folder hierarchy in one and not the other)

However, I don't know how to fix it... What is happening is that my log info shows that I'm loading: /static/pgbfiles/medicine.png -- this is working for the index.html
/publicacoes/static/pgbfiles/img.css -- for the second page named publicacoes.html

What I would like to have is all the static files in one single folder. They are already there but I don't know how to tell Django that they are all there for both html files...

I think I could add an additional folder for the "publicacoes" files, but that is not what I would like to do atm.

Thank you for any help!

What is the HTML you're using to load the CSS into publicacoes.html? I think you can probably fix the problem by changing that, but I'd need to see what you currently have first.

Is this what you mean? {% load static %}
And then on the particular places where I need: href="{% static 'pgbfiles/bootstrap.min.css' %}"

It might be something here since it works for index.html loading correctly but is adding the /publicacoes for the subpage

You're specifying a relative path (no preceding forward slash), so the request is going relative to the location of the page that contains it. Add a preceding slash to your href.

Thank you Glenn for answering it here and on SO.

As I've said there:

Thank you, that was the issue. I've tried it before, but I was only changing the info on the publicacoes.html when actually I had to change STATIC_URL on settings.py from 'static' to '/static'