简体   繁体   English

Django:图片未载入HTML模板

[英]Django: pictures not loading in HTML templates

When I add a random picture inside an HTML it doesn't load at all. 当我在HTML中添加随机图片时,它根本不会加载。 Here is how I set up my Django Project: 这是我设置Django项目的方法: 在此处输入图片说明

Inside of index.html: 在index.html内部:

{% load static %}
<img src='{% static "img/logo2.png" %}'> 

inside of Production, base and local.py I have this: 在Production,base和local.py内部,我有以下内容:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static/')

Thank you in advance if you can help me. 如果您能帮助我,请先谢谢您。

Here static folder was inside myapp but you have given root 这里的静态文件夹位于myapp内,但是您已经指定了root

 STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static/')

place the static folder into settings of blankapp 将静态文件夹放入blankapp的设置中

change the img src in HTML 更改HTML中的img src

check now 现在检查

Basic Configuration 基本配置

STATIC_URL = '/static/'


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

In your server try to access http://localhost:8000/static/img/logo2.png and check if the image is propely loaded, if the image loads so your confugiration at settings.py is right. 在您的服务器中,尝试访问http:// localhost:8000 / static / img / logo2.png并检查是否正确加载了图像,如果图像已加载,那么您在settings.py上的配置正确。

EDIT: You using heroku? 编辑:您使用heroku吗? im not sure if you will e able to use your static files with django at heroku, probabily use some external static server as Amazon S3 我不确定您是否能够在heroku上使用带有django的静态文件,可能会使用某些外部静态服务器作为Amazon S3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM