简体   繁体   English

Django 在模板下找不到图片

[英]Django cannot find images under templates

So I am creating a Django project and put an image on an HTML page.所以我正在创建一个 Django 项目并将图像放在 HTML 页面上。 Issue is that the server cannot see it.问题是服务器看不到它。

This is my folder structure: mysite/app/templates/app/img/pic.png这是我的文件夹结构: mysite/app/templates/app/img/pic.png

The HTML page is in: mysite/app/templates HTML 页面位于: mysite/app/templates

I tried many different scr formats, such as: <img src="../app/img/pic.png"> but it did not work.我尝试了许多不同的 scr 格式,例如: <img src="../app/img/pic.png">但它不起作用。

You have to set STATICFILES_DIRS to add the folder that you want, or try to put all the static in a folder and reference it in STATICFILES_DIRS , like this:您必须设置STATICFILES_DIRS以添加您想要的文件夹,或者尝试将所有 static 放在一个文件夹中并在STATICFILES_DIRS中引用它,如下所示:

STATICFILES_DIRS = [
    BASE_DIR / "static",
    '/var/www/static/',
]
  • the first url will be <the root of your project>/static第一个 url 将是<the root of your project>/static
  • the second url will be that exactly.第二个 url 就是这样。

For more info you have the docs right here: https://docs.djangoproject.com/en/3.2/howto/static-files/#managing-static-files-eg-images-javascript-css有关更多信息,您可以在此处查看文档: https://docs.djangoproject.com/en/3.2/howto/static-files/#managing-static-files-eg-images-javascript-css

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

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