在服务器上运行项目时,我看不到我的静态文件(在我的桌面上没问题)。
当我在浏览器上查找图片路径时,我有
/static/app_name/images/image_name
我的图片存储在
www.mydomain.com/xxx/xxx/static/app_name/images/image_name
我尝试从
调整settings.py'/static/'
到
'/xxx/xxx/static/'
但由于我的浏览器上的图片路径仍然是
,它似乎没有任何效果/static/app_name/images/image_name and not /xxx/xxx/static/app_name/images/image_name
我在这里错过了什么吗?
感谢您的帮助!
答案 0 :(得分:1)
更改STATIC_URL = '/static/'
只会更改网址,而不会更改实际投放图片的位置。
确保STATIC_ROOT
指向/path/to/www.mydomain.com/xxx/xxx/static/
,并确保 使用settings.py
中的硬编码路径,不类似
os.path.join(os.path.dirname(__file__, 'static'))
然后在你的模板中
<!-- either -->
<img src="{{ STATIC_URL }}img/my_image.png" />
<!-- or -->
{% load static %}
<img src="{% static 'img/my_image.png' %}" />
此外,请确保您正在运行python manage.py collectstatic
以收集所有应用中的所有静态文件,以将其放入STATIC_ROOT
目录。
根据您使用的服务器,确保您有一个指向静态目录的路径别名。例如,在/sites-available/www.mydomain.com中的Apache中,确保存在此Alias
指令
<VirtualHost *:80>
...
Alias /static /path/to/www.mydomain.com/xxx/xxx/static/
...
</VirtualHost>