问题描述
因此,在这里我使用django编写了一个简单的新闻应用程序,但是模板似乎无法正常工作,它没有显示从 views.py
传递的任何输出.我已经检查了 settings.py
, INSTALLED_APPS
设置是否有效
So, here I was writing a simple news app using django, but template does not seem to be working,it's not showing any output that has been passed from views.py
.I have checked settings.py
,INSTALLED_APPS
settings it's working
views.py
from django.shortcuts import render,HttpResponse
import bs4
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen
def get_news():
news_link = "https://news.google.com/news/rss"
page = urlopen(news_link)
xml_page = page.read()
page.close()
soup_page = soup(xml_page,"xml")
news_list = soup_page.findAll("item")
return news_list
def index(request):
news_list = get_news()
return render(request,'main/index.html',{'news_list' : news_list})
和模板
{% for news in news_list %}
<h3> {{ news.title.text }} </h3>
<h3> {{ news.pubDate.text }} </h3>
{% endfor %}
如果我从render()字典传递了任何其他数据,它可以正常工作,但是在传递列表时,它不会在模板中显示任何数据
If I pass any other data from render() dictionary, it is working but when passing lists,it's not showing any data in template
我尝试检查列表是否为None或为空,但没有,但不为空.谢谢!
I tried checking if the list is None or empty but no,it's not empty.Thank you!
推荐答案
您得到的内容类似于(bs4.element.Tag实例的 __ str __
):
You got something like (__str__
of bs4.element.Tag instance):
<标题> Lorem存有,悲...</标题>中< LINK> HTTPS:?//news.google.com/__i/rss/rd/articles/CBMiaGh0dHBzOi8vd3d3LmNubi5jb20vMjAyMC8wMi8wNS9wb2xpdGljcy9kb25hbGQtdHJ1bXAtbmFuY3ktcGVsb3NpLXN0YXRlLW9mLXRoZS11bmlvbi1hbmFseXNpcy9pbmRleC5odG1s0gFsaHR0cHM6Ly9hbXAuY25uLmNvbS9jbm4vMjAyMC8wMi8wNS9wb2xpdGljcy9kb25hbGQtdHJ1bXAtbmFuY3ktcGVsb3NpLXN0YXRlLW9mLXRoZS11bmlvbi1hbmFseXNpcy9pbmRleC5odG1s OC = 5℃;/link> ;,< guid isPermaLink ="false"> 52780574934659</guid> ;,< pubDate> Wed,05 Feb 2020 08:15:00 GMT</pubDate> ;,< description>< ol& amp; amp; amp; amp; amp;; GT;&安培; LT;李&安培; GT;&安培; LT; A HREF = https://news.google.com/__i/rss/rd/articles/CBMiaGh0dHBzOi8vd3d3LmNubi5jb20vMjAyMC8wMi8wNS9wb2xpdGljcy9kb25hbGQtdHJ1bXAtbmFuY3ktcGVsb3NpLXN0YXRlLW9mLXRoZS11bmlvbi1hbmFseXNpcy9pbmRleC5odG1s0gFsaHR0cHM6Ly9hbXAuY25uLmNvbS9jbm4vMjAyMC8wMi8wNS9wb2xpdGljcy9kb25hbGQtdHJ1bXAtbmFuY3ktcGVsb3NpLXN0YXRlLW9mLXRoZS11bmlvbi1hbmFseXNpcy9pbmRleC5odG1s?oc=5" 目标= _空白" &安培;gt;特朗普制造在《国情咨文》中令人眼花vis乱的改换音高</a& amp; nbsp;& nbsp;& lt; font color =#6f6f6f"" CNN</字体& lt;/li& gt;& lt; li& gt;& lt; a href ="https://news.google.com/__i/rss/rd/articles/CBMiK2h0dHBzOi8vd3d3LnlvdXR1YmUuY29tL3dhdDN1NQZlYzAzQNlQDQZlQnQAzNQDQ1LQNQDzDQ1DQ1DzDQ1DQ1DQ1D9D9D国庆国庆日报(英语:Football)& lt;/li& lt; li& gt;& lt; a href =" https://news.google.com/__i/rss/rd/articles/CBMiK2h0dHBzOi8vd3d3LnlvdXR1YmUuY29tL3dhdDN= 5"target =" _ blank& gt;南希·佩洛西(Nancy Pelosi)撕毁了特朗普的国情咨文(</a)& nbsp;& amp; nbsp;& lt; font color ="#6f6f6f"& gt; CNN& lt;/font& gt;& lt;/li& gt;& lt; li& gt;& lt; a href ="https://news.google.com?/__ I/RSS/次/篇/CBMiZWh0dHBzOi8vd3d3Lm5iY25ld3MuY29tL3RoaW5rL29waW5pb24vc3RhdGUtdW5pb24taXNuLXQtc3Ryb25nLXRydW1wLXByZXRlbmRzLWl0LXMtYmV0dGVyLW5jbmExMTI5OTIx0gEtaHR0cHM6Ly93d3cubmJjbmV3cy5jb20vdGhpbmsvYW1wL25jbmExMTI5OTIx OC = 5" 目标= _空白" &放大器; GT;的国情咨文是没有那么强,因为特朗普假装,但它比民主党人说,&放大器; LT;/A&安培;gt;& nbsp;& amp; nbsp;& lt; fontcolor =#6f6f6f"& gt; NBC新闻</font& gt;& lt;/li& gt;& lt; li& gt;& lt; a href ="https://news?.google.com/__ I/RSS/次/篇/CBMicWh0dHBzOi8vbnlwb3N0LmNvbS8yMDIwLzAyLzA1L3N0YXRlLW9mLXRoZS11bmlvbi1hZGRyZXNzLXRydW1wLWhpdHMtZ3JhbmQtc2xhbS13aGlsZS1wZWxvc2ktanVzdC1sb29rcy1sb29rcy1zYWQv0gF1aHR0cHM6Ly9ueXBvc3QuY29tLzIwMjAvMDIvMDUvc3RhdGUtb2YtdGhlLXVuaW9uLWFkZHJlc3MtdHJ1bXAtaGl0cy1ncmFuZC1zbGFtLXdoaWxlLXBlbG9zaS1qdXN0LWxvb2tzLWxvb2tzLXNhZC9hbXAv OC = 5" 目标= _空白" &放大器; GT;的国情咨文:特鲁姆普打大满贯,而佩洛西只是外表看起来悲伤和放大器; LT;/A&放大器;& nbsp;& amp; nbsp;字体颜色=#6f6f6f"& gt;纽约邮报</font& gt;& lt;/li& gt;&安培; LT;李&安培; GT;&安培; LT; A HREF =https://news.google.com/__i/rss/rd/articles/CBMiZmh0dHBzOi8vd3d3LmZveG5ld3MuY29tL29waW5pb24vbGVzbGllLW1hcnNoYWxsLXNvdHUtYWRkcmVzcy1jb250aW51ZXMtdHJ1bXBzLXJvbGUtYXMtZGl2aWRlci1pbi1jaGllZtIBamh0dHBzOi8vd3d3LmZveG5ld3MuY29tL29waW5pb24vbGVzbGllLW1hcnNoYWxsLXNvdHUtYWRkcmVzcy1jb250aW51ZXMtdHJ1bXBzLXJvbGUtYXMtZGl2aWRlci1pbi1jaGllZi5hbXA?oc = 5"target =" _ blank& amp; b& Leslie Marshall:SOTU地址Trump继续; font color =#6f6f6f"> Fox News</font& gt;</li& gt;< li& gt;< strong& lt;& lt; a href ="https://news.google.com/stories/CAAqOQgKIjNDQklTSURvSmMzUnZjbmt0TXpZd1NoTUtFUWlEamE3N2pvQU1FU1YxUEo2emxrYWRLQUFQAQ?oc=5" target ="_ blank"& gt;& gt;& gt;/strong& lt;/li& lt;/ol& gt;</description>,<源url ="https://www.cnn.com"> CNN</来源>
存储在新闻
中.
您不能使用 {{news.title.text}}
访问该实例.这就是为什么您最终在模板中得到空结果的原因.尝试{{新闻}},您将看到其中的内容.
You can't access this instance with {{ news.title.text }}
. That's why you end up with a empty result in your template. Try {{ news }} an you'll see what's in there.
要么创建自定义模板过滤器,要么可以从该实例中提取必要的数据-> https://docs.djangoproject.com/en/3.0/howto/custom-template-tags/
Either you create a custom template filter, which can extract the necessary data from that instance --> https://docs.djangoproject.com/en/3.0/howto/custom-template-tags/
或者您将 get_news
方法中的数据准备到python结构 dicts列表
Or you prepare the data in your get_news
method into a python structure list of dicts
工作示例:
def get_news():
news_link = "https://news.google.com/news/rss"
page = urlopen(news_link)
xml_page = page.read()
page.close()
soup_page = soup(xml_page, "xml")
news_list = map(
lambda item: {'title': item.title.text, 'date': item.pubDate.text},
soup_page.findAll("item"))
return news_list
在模板中:
{% for news in news_list %}
<h3> {{ news.title }} </h3>
<h3> {{ news.date }} </h3>
{% endfor %}
这篇关于为什么Django模板未显示任何输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!