[英]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 - 我想在Django中显示列表的内容,并在模板中使用它-
<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. (由于最后一个问题,我添加了保险箱)为了进行测试,我还将searchTerm设置为也等于“ Math 51”。 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 - getAuthorName也是这个-
def getAuthorName(soup):
temp = soup.findAll('li', {"class": "efb9"})
storeOfAuthorNames = []
reName = re.compile('Author: (\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. 我知道一个事实,如果我要输入MATH 51作为searchTerm,storeOfAuthorNames将返回一个值(在本例中为“ Levandosky”),因为我已经打印到控制台并显示了该值。
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: Levandosky </li>,
<li class="efb9"> Edition: </li> ]
The problem is because mistyping. 问题是因为迷惑。 Some spaces are in the wrong place.
一些空间放置在错误的位置。 use this
用这个
{% for author in authors %}
<li>{{ author|safe }}</li>
{% endfor %}
The cleanest way to display a list in django is: 在django中显示列表的最干净方法是:
{{ var|unordered_list }}
You can also add safe
if you want to display html: 如果要显示html,也可以添加
safe
:
{{ mylist|safe|unordered_list }}
NOTE: on the last code i'm not 100% sure if safe
is before or after unordered_list
注意:在最后一个代码上,我不确定100%是否
safe
在unordered_list
之前或之后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.