简体   繁体   English

如何在 html (django) 中显示 img

[英]How do I display a img in html (django)

html file: html 文件:

<!DOCTYPE html>
<html>
<head>


    <title>Resume Homepage</title>

</head>

<body>
<img src="main_photo.jpg" alt="Image Unable To Be Displayed">

</body>


</html>

Note the photo is in the same directory as the html file so why wont it display?请注意,照片与 html 文件位于同一目录中,为什么不显示? I keep just getting 404 errors.我不断收到 404 错误。

I am also near positive the name of the photo file is correct as its very simple how could I mess that up.我也几乎肯定照片文件的名称是正确的,因为它非常简单,我怎么能把它搞砸。

This is how i got it working.这就是我让它工作的方式。

settings.py设置.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

MEDIA_ROOT = (
BASE_DIR
)


MEDIA_URL = '/media/'

models.py...模型.py...

image = models.ImageField(upload_to='img')

urls.py(project's) urls.py(项目的)

if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

template (.html)模板 (.html)

<img src="{{ post.image.url }}" alt="img">

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

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