1

This is sort of an odd question, not sure if it's a bug or it's something silly I've done.

In my views.py,

def display(request):
    altTrue = []
    altFalse = []
    altTrue, altFalse = altCheck(soup)
    results = { 'alttrue' : altTrue,
                'altfalse' : altFalse,
              }
    return render(request, 'results.html', results)

def altCheck(soup):
    alttrue = []
    altfalse = []
    #The multimedia tags we're searching for
    multimedialist = ['img','input','area']
    #Return all tags from multimedia list and check if alt is present in them
    for tag in multimedialist:
        for incodetag in soup.findAll(tag):
            if incodetag.get('alt') is None:
                altfalse.append(incodetag)
            else:
                alttrue.append(incodetag)
    return alttrue, altfalse

In my results.html,

{{ alttrue }}   <!-- Prints out the list successfully -->
{% for tag in alttrue %}   <!--Prints out [] but with correct count-->
    {{ tag }}
{% endfor %}

I am validating whether the list is empty or not before trying to print but still. The moment I run the list through a for loop, I get as many []s as the number of actual elements in the list.

Strangely, when I tried the same thing on my Python interpreter, my result came out fine, regardless of whether I printed out the object directly or If I used a loop.

Am I missing something?

Thanks in advance!

7
  • Can you provide more information about {{ alttrue }}? Presumably it is a list of strings? Commented Oct 18, 2015 at 14:32
  • Thanks for the comment @lambo477. The list contains html tags, but yes, essentially strings. An example of the list being [<img height="50" src="assets/images/logo.png" width="50"/>, <input class="form-control" placeholder="Search" type="text"/>] Commented Oct 18, 2015 at 14:34
  • dispay view has to take request. Commented Oct 18, 2015 at 14:37
  • @pythad Thank you, but I have added it in my actual code. The one I've posted here, I've forgotten to put it. Just a typo. It actually takes request in my code. Thanks for pointing out though, will put it in to avoid confusion. Commented Oct 18, 2015 at 14:39
  • 1
    @RohitNambiar, The problem is that [<img height="50" src="assets/images/logo.png" width="50"/>, <input class="form-control" placeholder="Search" type="text"/>] is not a list of strings, the quotes are necessary, If am not mistaken it's list of BS objects and this can explain why you can't loop over it so easy. Commented Oct 18, 2015 at 14:53

1 Answer 1

3

The problem occurs because your lists altTrue and altFalse are not lists of strings, but are actually lists of bs4.element.Tag (ie Beautiful Soup element tags).

For example, if you were to alter your request function to

def display(request):          
  content = '''
  <div>
      <img height="50" src="assets/images/logo.png" width="50"/>
  </div>
  '''
  soup = BeautifulSoup(content)
  altTrue = soup.findAll('img')
  results = {'alttrue': altTrue}
  return render(request, 'results.html', results)

Then:

{% for tag in alttrue %}
  {{ tag }}
{% endfor %}

...does not display anything because you are working with a list of bs4.element.Tags. If instead, you were to change your display function to

def display(request):          
    altTrue = ['one', 'two']
    results = {'alttrue': altTrue}
    return render(request, 'results.html', results)

Then the HTML:

{% for tag in alttrue %}
  {{ tag }}
{% endfor %}

Will output one, two as required.

So you need to alter your display function to:

def display(request):
    altTrue = []
    altFalse = []
    altTrue, altFalse = altCheck(soup)
    altTrue = [item.name for item in altTrue]
    altFalse = [item.name for item in altFalse]    
    results = { 'alttrue' : altTrue,
                'altfalse' : altFalse,
              }
    return render(request, 'results.html', results)  
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I had a feeling they weren't strings. However, your solution doesn't seem to be working for me. When I use your solution and loop through the result, I get None None. EDIT: Instead of your solution, when appending the items into the list, I simply parsed them as string and now it works fine. Thank you for your help @lambo477 and @pythad!

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.