Get the first image src from a post

53 views
Skip to first unread message

Robin Lery

unread,
Jun 15, 2015, 8:00:04 AM6/15/15
to django...@googlegroups.com

I have a model to write blogs. In that I'm using a wysiwyg editor for the Blog's descritpion, through which I can insert an image within the description.

class Blog(models.Model):
    title = models.CharField(max_length=150, blank=True)
    description = models.TextField()
    pubdate = models.DateTimeField(default=timezone.now)
    publish = models.BooleanField(default=False)

In the admin it looks like this:

enter image description here

What I want is to get first image inside the description of the blog, and present it in the template like this:

enter image description here

How do get the img src from the description of the blog to use it in the template? Your help and guidance will be very much appreciated. Thank you.

views.py:

def blog(request):
    blogs = Blog.objects.all()

    return render(request, 'blogs.html', {
        'blogs':blogs
        })

template:

  {% for blog in blogs %}
  <div class="blog">
      <p class="blog_date"> {{blog.pubdate}} </p>
      <h2 class="blog_title"> {{blog.title}} </h2>
      <img src="{{STATIC_URL}} ###img src to be included" class="blog_image img-responsive img-thumbnail">


          <a href="blog_detail.html" class="blog_read_more btn btn-info">Read more</a>
      <div class="container">
          <p class="divider">***</p>
      </div>
  </div>
  {% endfor %}

Vijay Khemlani

unread,
Jun 15, 2015, 8:25:15 AM6/15/15
to django...@googlegroups.com
If the description is stored as HTML I guess you could use something like BeautifulSoup or pyquery to parse it and find the tag

from bs4 import BeautifulSoup

soup = BeautifulSoup(blog.description)
image_src = soup.find('img')['src']


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B4-nGo4ZJmArnQ2nvF5f0tp83HA7LJTxdywtO8%2BxsBVp2211g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Robert librado

unread,
Jun 15, 2015, 10:37:28 AM6/15/15
to django...@googlegroups.com
very nice work
>> <https://groups.google.com/d/msgid/django-users/CA%2B4-nGo4ZJmArnQ2nvF5f0tp83HA7LJTxdywtO8%2BxsBVp2211g%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CALn3ei242vCJfTSrUtWTKbNO%2BXCDkPdn7pOJzN%3DhkXoR0CjT5Q%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages