Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fetching Images from DB in Django
#1
It's a blog post and I already uploaded images to each post on the Admin page. The images aren't showing in the template, for some reason.
Project tree:
|______projectDirectory
|______media
___________|______imgs
|______blogApp

models.py
class Post(models.Model):
    title = models.CharField(max_length=140) 
    body = models.TextField()  
    date = models.DateTimeField()
    blog_image = models.ImageField(blank=True, upload_to='imgs')
urls.py
urlpatterns = [
          path('<int:pk>/', DetailView.as_view(model= Post, template_name= 'blog/post.html')),
]
post.html
{% block content %}
{% if post %}  
    <div>
    <h1>{{ post.title }}</h1>
    <h5>{{ post.date }}</h5> <br><br>
    <div>    
        <img src="{{ MEDIA_URL }}{{ post.blog_image }}" alt={{ post.blog_image.url }} />       
        <br><br>
    </div>
    <div>{{ post.body|safe|linebreaks }} </div>     
    </div>
{% endif %}
{% endblock %}
Reply
#2
<img src="{{ post.blog_image.url }} "/>
or
<img src="/media/{{ post.blog_image}} "/>
Dexty likes this post
Reply
#3
Does no one seriously have an answer to this? Or is it a Django induction ritual Dodgy ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django images will not load pythonpaul32 3 2,725 Jul-20-2023, 10:30 AM
Last Post: Gaurav_Kumar
  fetching, parsing data from Wikipedia apollo 2 3,564 May-06-2021, 08:08 PM
Last Post: snippsat
  display local images on django website mp3909 2 6,335 Apr-01-2020, 11:18 AM
Last Post: mp3909
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,334 Jun-30-2019, 12:21 AM
Last Post: scidam
  Fetching and Parsing XML Data FalseFact 3 3,279 Apr-01-2019, 10:21 AM
Last Post: Larz60+
  how to make my product description fetching function generic? PrateekG 10 6,097 Jun-29-2018, 01:03 PM
Last Post: PrateekG
  Getting 'list index out of range' while fetching product details using BeautifulSoup? PrateekG 8 8,188 Jun-06-2018, 12:15 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020