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']
On Mon, Jun 15, 2015 at 8:59 AM, Robin Lery <robinlery@gmail.com> wrote:
--I have a model to write blogs. In that I'm using a wysiwyg editor for the
Blog'sdescritpion, through which I can insert an image within thedescription.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:
What I want is to get first image inside the description of the blog, and present it in the template like this:
How do get the
imgsrcfrom 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 %}
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.
For more options, visit https://groups.google.com/d/optout.


No comments:
Post a Comment