Answer the question
In order to leave comments, you need to log in
Why doesn't Django show media on localhost?
Everything is spelled out, the pillow is installed. Saving an image to a folder is no problem. But when I try to display the uploaded image on the page, I get "Not Found: /media/img.jpg" in the terminal.
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
class Post(models.Model):
title = models.CharField(max_length=150, db_index=True)
slug = models.SlugField(max_length=150, blank=True, unique=True)
body = models.TextField(blank=True, db_index=True)
tags = models.ManyToManyField('Tag', blank=True, related_name='posts')
image = models.ImageField(blank=True)
urlpatterns +=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
{%extends 'blog/base_blog.html'%}
{%block content%}
<h1 class="mt-5">
{{post.title}}
</h1>
<p>{{post.body}}</p>
{{post.image.url}}
<img src="{{post.image.url}}"/>
{%endblock%}
{{post.image.url}}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question