praks5432
praks5432

Reputation: 7792

Django Templates display list

This is sort of a follow up to a previous question, but I'll repost all of my code here-

I want to display the contents of a list in Django and I'm using this in my template -

<body>
  <h1>Testing the class page backend</h1>
  <ul>
    { % for author in authors% }
      <li>{{ author|safe }}</li>
    { % endfor % }
  </ul>
</body> 

(I added the safe as a result of the last question) For testing, I'm setting searchTerm as equal to "Math 51" as well. authors is supplied by this function -

def getAllInformation(searchTerm, template_name):
    searchTerm = "MATH 51"
    nameAndNumberStore = modifySearchTerm(searchTerm)
    url = modifyUrl(nameAndNumberStore)
    soup = getHtml(url)
    information = []
    if (checkIfValidClass(soup,nameAndNumberStore)):
        storeOfEditions = getEdition(soup)
        storeOfAuthorNames = getAuthorName(soup)
        storeOfBookNames = getBookNames(soup)
        storeOfImages = getImages(soup)
    information.append(storeOfAuthorNames)  #REMEMBER this is a list of two lists 
    information.append(storeOfEditions)
    return render_to_response(
      template_name,
      {'authors': storeOfAuthorNames},
    )

getAuthorName is also this -

def getAuthorName(soup):
    temp = soup.findAll('li', {"class": "efb9"})
    storeOfAuthorNames = []
    reName = re.compile('Author:&nbsp;(\w+)')
    for i in range(len(temp)):
        if (i % 2 == 0):
            name = temp[i].string
            editedName = re.findall(reName, name)
            editedName = editedName[0]
            storeOfAuthorNames.append(editedName)
    return storeOfAuthorNames

I know for a fact that if I were to enter MATH 51 as a searchTerm, storeOfAuthorNames returns a value (in this case 'Levandosky'), because I've printed to console and shown that.

Hence, I have no clue why the template code that I've provided doesn't display the author name, and only displays the template tags.

Can anyone help?

EDIT- Also contents of temp -

[ <li class="efb9"> Author:&nbsp;Levandosky </li>,
  <li class="efb9"> Edition:&nbsp; </li> ]

Upvotes: 1

Views: 16698

Answers (2)

balazs
balazs

Reputation: 5788

The problem is because mistyping. Some spaces are in the wrong place. use this

{% for author in authors %}
    <li>{{ author|safe }}</li>
{% endfor %}

Upvotes: 6

Diego Navarro
Diego Navarro

Reputation: 9704

The cleanest way to display a list in django is:

{{ var|unordered_list }}

You can also add safeif you want to display html:

{{ mylist|safe|unordered_list }}

NOTE: on the last code i'm not 100% sure if safe is before or after unordered_list

Upvotes: 5

Related Questions