繁体   English   中英

Django模板显示列表

[英]Django Templates display list

这是对上一个问题的后续操作,但我将在此处重新发布所有代码-

我想在Django中显示列表的内容,并在模板中使用它-

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

(由于最后一个问题,我添加了保险箱)为了进行测试,我还将searchTerm设置为也等于“ Math 51”。 作者由此功能提供-

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也是这个-

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

我知道一个事实,如果我要输入MATH 51作为searchTerm,storeOfAuthorNames将返回一个值(在本例中为“ Levandosky”),因为我已经打印到控制台并显示了该值。

因此,我不知道为什么我提供的模板代码不显示作者名称,而只显示模板标签。

有人可以帮忙吗?

编辑-临时内容-

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

问题是因为迷惑。 一些空间放置在错误的位置。 用这个

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

在django中显示列表的最干净方法是:

{{ var|unordered_list }}

如果要显示html,也可以添加safe

{{ mylist|safe|unordered_list }}

注意:在最后一个代码上,我不确定100%是否safeunordered_list之前或之后

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM