我不会在我的数据库中保存数据。我正在使用一个api服务,我不想将这些api数据保存在我的数据库中。我只想在html模板中显示这些数据。这个问题只是首先在模板中显示for循环的数据,在模板中我可以从我的终端看到所有的for循环数据。我想在我的html模板中显示我的所有for循环数据。这是我的代码:
views.py:
for i in results[search_type]:
if search_type == "organic_results":
title = i["title"]
print(title)
context = {"title":title}我知道我可以使用追加方法,但它将模板中的所有数据一起显示为列表。我想在我的模板中使用如下for循环的主题:
html
{%for i in title %}{{i}}{%endfor%}我的最终结果是:
Facebook - Log In or Sign Up
https://www.facebook.com/
Newsroom | Meta - Facebook
https://about.fb.com/news/
Facebook - Apps on Google Play
https://play.google.com/store/apps/details?id=com.facebook.katana&hl=en_US&gl=US
Facebook - Wikipedia
https://en.wikipedia.org/wiki/Facebook
Facebook Careers | Do the Most Meaningful Work of Your ...
https://www.facebookcareers.com/
Facebook - Twitter
https://twitter.com/facebook为什么我的模板中只显示了for循环的第一个数据?
发布于 2022-01-28 13:45:12
您在模板上只显示一个标题,因为您只将一个标题传递给上下文。
你可以在你的观点中用到这样的东西:
titles = []
for i in results[search_type]:
if search_type == "organic_results":
title = i["title"]
titles.append(title)
print(title)
context = {"titles":titles}并使用for循环在模板上迭代:
{%for title in titles %}{{ title }}{%endfor%}https://stackoverflow.com/questions/70894656
复制相似问题