1

Iam trying to parse html page and find all image tags and display it in django template

view

import urllib2
from BeautifulSoup import BeautifulSoup
page = urllib2.urlopen("http://www.flipkart.com/")
soup = BeautifulSoup(page)
tags=soup.findAll('img')
template = get_template('welcome.html')            
variables = RequestContext(request,{'tags':tags})
output = template.render(variables)
return HttpResponse(output)

Template

{% block content %} 

<div class="row">
 <ul class="thumbnails">
 {% for row in tags %}
 <li >
   <span>Flash</span>
   <a href="#" class="thumbnail">
   {{ row }}
   </a>
</li>

{% endfor %}
</ul>
</div>

{% endblock %}

i printed tags

 [<img src="http://passets-cdn.pinterest.com/images/search.gif" alt="" />, <img        src="http://media-cache2.pinterest.com/upload/422281184577033_NvxwzARh_b.jpg"  alt="#wedding #bouquet #flowers" class="PinImageImg" style="height: 288px;" />, <img src="http://media-cache0.pinterest.com/avatars/heygirlfriend-33.jpg" alt="Profile picture of Heather Carpenter" />, <img src="http://media-cache2.pinterest.com/avatars/lilizzy08_1330284092.jpg" class="profile user_image" alt="Profile picture of JoAnn Boyle Barker" />, <img src="http://media-cache2.pinterest.com/avatars/camelotparty_1330114747.jpg" class="profile user_image" alt="Profile picture of Camelot Party" />, <img src="http://media-cache5.pinterest.com/avatars/mamababe13_1327965590.jpg" class="profile user_image" alt="Profile picture of Irene Hardin Sanchez" />, <img src="http://media-cache5.pinterest.com/avatars/mpowers213_1333304368.jpg" class="profile user_image" alt="Profile picture of Maggie Powers" />, <img src="http://media-cache7.pinterest.com/avatars/apricot1026_1334529181.jpg" class="profile user_image" alt="Profile picture of Michelle Nadel" />, <img src="http://media-cache4.pinterest.com/upload/169025792234929326_WtMMM67J_b.jpg" alt="Dr. Oz Metabolism Booster" class="PinImageImg" style="height: 256px;" />, <img src="http://media-cache6.pinterest.com/avatars/karenmigala_1332630951.jpg" alt="Profile picture of Karen Migala" />, <img src="http://media-cache9.pinterest.com/avatars/dutchjohnson-26.jpg" class="profile user_image" alt="Profile picture of Dutch Johnson" />]

when i tried to print the tags there is lot of img tags, But iam getting an empty list in browser(when displayed).

HTML page Displays

multple empty lists like [] [] [] [] [] []

Rendered Content

<li >
<span>Flash</span>
<a href="#" class="thumbnail">
[]

</a>
</li>

 <li >
 <span>Flash</span>
 <a href="#" class="thumbnail">
 []

</a>
</li>

 <li >
 <span>Flash</span>
 <a href="#" class="thumbnail">
 []

 </a>
 </li>
 <li >
<span>Flash</span>
 <a href="#" class="thumbnail">
 [] 
</a>
</li>
 <li >
 <span>Flash</span>
 <a href="#" class="thumbnail">
 []  
</a>
</li>

Please help

3
  • 1) Post content of tags variable from debugger after line tags=soup.findAll('img') 2) Post content of rendered html page (part inside <div class="row") Commented May 14, 2012 at 11:51
  • I had posted the necessary contents Commented May 14, 2012 at 12:13
  • Also put rendered content in output Commented May 14, 2012 at 12:15

1 Answer 1

3

Ah, that's because the Tag of BeautifulSoup is callable, thus Django template invoke it directly

class Tag(PageElement):
    ...
    def __call__(self, *args, **kwargs):
        """Calling a tag like a function is the same as calling its
        findAll() method. Eg. tag('a') returns a list of all the A tags
        found within this tag."""
        return apply(self.findAll, args, kwargs)

For newer version of Django, you could set do_not_call_in_templates to True to avoid invoking.

from BeautifulSoup import Tag

tags=soup.findAll('img')
Tag.do_not_call_in_templates = True
# render
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.