简体   繁体   中英

How do I display a img in html (django)

html file:

<!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? I keep just getting 404 errors.

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

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...

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

urls.py(project's)

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

template (.html)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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