2

I'm a little confused with Django's static files.

I understand they are not served in development however in my production environment I have tried everything to change the URL for the files which are being server. It just doesnt make any sence to me.

Everything is still being served via /static/ with my URL's even after changing STATIC_URL.

settings.py

STATIC_ROOT = os.path.join(BASE_DIR, 'dsadsa')
STATIC_URL = '/ddd/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'app/static'),
]

From those setting I expect all my static files to end up inside of a directory called dsadsa and server with a url www.example.com/ddd/file.css however this is just not the case they always get served via www.example.com/static/file.css from the directory staticfiles.

I'm running this project in Heroku with no Debug = False

Can anyone please help me understand what on earth I'm missing here?

Update:

I'm using {% load static %} within the templates.

Example template usage:

<img alt="background" src="{% static "img/inner-6.jpg" %}" />
Kyias
  • 166
  • 2
  • 11
  • Show an example of the template, please. – Daniel Roseman Jun 20 '18 at 08:32
  • Did you restart the server, and *hard* refreshed the page? – Willem Van Onsem Jun 20 '18 at 08:33
  • Template usage added. Yes restart of Dynos makes no difference. – Kyias Jun 20 '18 at 08:41
  • Note: I have just found this so I'm trying to implement. https://stackoverflow.com/questions/21141315/django-static-files-on-heroku – Kyias Jun 20 '18 at 08:48
  • Update: That did not work. Still always served from `/static/` what am I missing? – Kyias Jun 20 '18 at 09:04
  • It's weird. Is there other static settings? – seuling Jun 20 '18 at 09:12
  • @seuling I have searched the whole project. No other static settings anywhere. Nothing I do will change the `/static/` URL in the references in the HTML. Ever :S – Kyias Jun 20 '18 at 13:45
  • It should change. I guess it's not your setting's problem. What is your environment? Can you runserver in your localhost? – seuling Jun 20 '18 at 16:34
  • I can runserver in my local environment. The project is on Heroku. Nothin I do changes the public facing URI. :S I have given up for now, – Kyias Jun 21 '18 at 12:38
  • For the sake of documenting another source of error, at least when not deploying to heroku but hosting yourself (e.g. uWSGI-NGINX): Simply adding a subdirectory (e.g. `/mydjangoapp` as in `www.mydomain.com/mydjangoapp`) to FORCE_SCRIPT_NAME, STATIC_URL, STATIC_FILES_URL, ADMIN_MEDIA_PREFIX,... has not gained anything for me as well. Finally I found removing the left-over `django_heroku.settings(locals())`-line finally provided the subdirectory-prefix in the static-file links – Phil Oct 31 '19 at 22:20

1 Answers1

0

I think what you're looking for...maybe

STATIC_DIR = os.path.join(BASE_DIR, 'ddd')
STATIC_URL = '/ddd/'
STATICFILES_DIRS = [STATIC_DIR,]

The above will link {% load staticfiles %} in your base.html to the static folder which houses img/css/etc.

This will point all references to {% load staticfiles %} to the static folder under your base project.

Linus
  • 411
  • 5
  • 14
  • I will give this a try. Why `{% load staticfiles %}` and not `{% load static %}` - Also this will fail to collect my static files in the app directory as its not stated in `STATICFILES_DIRS`? – Kyias Jun 20 '18 at 13:00
  • This has also made no difference what so ever. – Kyias Jun 20 '18 at 13:09
  • Did you run "python manage.py collectstatic". That will put all the contents of your static files into the static root. It should be noted that the STATIC_ROOT should be a new file under the root of your project, same level as the original 'static' folder. – Linus Jun 23 '18 at 19:13