user2950162
user2950162

Reputation: 1011

Django static files not loading (production)

I can not see my static files when running the project on the server (at my desktop it was ok).

When I look for the picture path on my browser I have

/static/app_name/images/image_name

My images are stored at

www.mydomain.com/xxx/xxx/static/app_name/images/image_name

I tried to adjust the settings.py from

'/static/'

to

'/xxx/xxx/static/'

But it seems to have no effect as the images path on my browser are still

/static/app_name/images/image_name and not /xxx/xxx/static/app_name/images/image_name

Am I missing something here?

Thanks for any help!

Upvotes: 0

Views: 1868

Answers (1)

OozeMeister
OozeMeister

Reputation: 4859

Changing STATIC_URL = '/static/' is only going to change the URL, not where the images are actually served.

Make sure that STATIC_ROOT is pointing to /path/to/www.mydomain.com/xxx/xxx/static/ and make sure you are using hard-coded paths in your settings.py, not something like

os.path.join(os.path.dirname(__file__, 'static'))

Then in your templates

<!-- either -->
<img src="{{ STATIC_URL }}img/my_image.png" />
<!-- or -->
{% load static %}
<img src="{% static 'img/my_image.png' %}" />

Also, be sure you're running python manage.py collectstatic to collect all of the static files from all of your apps to place them in the STATIC_ROOT directory.

Also

Depending on which server you're using, make sure you have a path alias that points to your static directory. For example, in Apache in your /sites-available/www.mydomain.com conf make sure that this Alias directive exists

<VirtualHost *:80>
    ...
    Alias /static /path/to/www.mydomain.com/xxx/xxx/static/
    ...
</VirtualHost>

Upvotes: 2

Related Questions