I am trying to pass a variable which has a html table into it as a HTML table in a Django template.
when I pass it though and mark it as | safe or turn auto escape off. all the HTML is inserted but the table is fully removed, does anyone know why and how I can turn it off?
import urllib.request
from bs4 import BeautifulSoup
tt_opener = urllib.request.build_opener()
tt_opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
tt_service = tt_opener.open('https://managed.mytalktalkbusiness.co.uk/network-status/')
tt_soup = BeautifulSoup(tt_service, "html.parser")
tt_data = tt_soup.table
template
<div id="TALK-TALK-Service" style="width:50vw; height:50vw; float:left;">
{{ TalkTalk |kksafe }}
</div>
the tt_data variable printed via shell
<table border="0" cellpadding="0" cellspacing="0" class="opaltable" width="100%"><th nowrap="nowrap"> </th><th nowrap="nowrap">Issue</th><th nowrap="nowrap">Services affected</th><th nowrap="nowrap">Location</th><th nowrap="nowrap">Last update</th><tr><td width="20"><img src="https://managed.mytalktalkbusiness.co.uk/images/redlight.gif"/></td><td width="350"><a href="https://managed.mytalktalkbusiness.co.uk/network-status-report.php?reportid=15462">incident 10574541 - Washington Exchange - no service</a></td><td>Extranet, Ethernet, EoFTTC & EFM via TalkTalk, DSL via TalkTalk</td><td width="100">n/a</td><td nowrap="nowrap" width="150">11th Oct 2017 12:53</td></tr><tr><td width="20"><img src="https://managed.mytalktalkbusiness.co.uk/images/redlight.gif"/></td><td width="350"><a href="https://managed.mytalktalkbusiness.co.uk/network-status-report.php?reportid=15448">Incident 10573277 - Network – P1 – Some TTB customers are experiencing Post Dial Delay SIP/VOE</a></td><td>SIP/VOE</td><td width="100">n/a</td><td nowrap="nowrap" width="150">11th Oct 2017 12:27</td></tr></table>
the html displayed on the webpage
<div id="TALK-TALK-Service" style="width:50vw; height:50vw; float:left;">
[ , Issue, Services affected, Location, Last update, <img src="https://managed.mytalktalkbusiness.co.uk/images/redlight.gif"><a href="https://managed.mytalktalkbusiness.co.uk/network-status-report.php?reportid=15462">incident 10574541 - Washington Exchange - no service</a>Extranet, Ethernet, EoFTTC & EFM via TalkTalk, DSL via TalkTalkn/a11th Oct 2017 12:53, <img src="https://managed.mytalktalkbusiness.co.uk/images/redlight.gif">, <img src="https://managed.mytalktalkbusiness.co.uk/images/redlight.gif">, <a href="https://managed.mytalktalkbusiness.co.uk/network-status-report.php?reportid=15462">incident 10574541 - Washington Exchange - no service</a>, <a href="https://managed.mytalktalkbusiness.co.uk/network-status-report.php?reportid=15462">incident 10574541 - Washington Exchange - no service</a>, Extranet, Ethernet, EoFTTC & EFM via TalkTalk, DSL via TalkTalk, n/a, 11th Oct 2017 12:53, <img src="https://managed.mytalktalkbusiness.co.uk/images/redlight.gif"><a href="https://managed.mytalktalkbusiness.co.uk/network-status-report.php?reportid=15448">Incident 10573277 - Network – P1 – Some TTB customers are experiencing Post Dial Delay SIP/VOE</a>SIP/VOEn/a11th Oct 2017 12:27, <img src="https://managed.mytalktalkbusiness.co.uk/images/redlight.gif">, <img src="https://managed.mytalktalkbusiness.co.uk/images/redlight.gif">, <a href="https://managed.mytalktalkbusiness.co.uk/network-status-report.php?reportid=15448">Incident 10573277 - Network – P1 – Some TTB customers are experiencing Post Dial Delay SIP/VOE</a>, <a href="https://managed.mytalktalkbusiness.co.uk/network-status-report.php?reportid=15448">Incident 10573277 - Network – P1 – Some TTB customers are experiencing Post Dial Delay SIP/VOE</a>, SIP/VOE, n/a, 11th Oct 2017 12:27]
</div>
Your tt_soup
variable is a Tag
object from bs4, not a string. You can check this in your view.
tt_soup = BeautifulSoup(tt_service, "html.parser")
print(type(tt_soup))
Since the tt_soup
object is callable, the Django template language is calling it when it renders the table, which gives the unexpected result. You can prevent this by convering tt_soup to a string in the view.
tt_soup = BeautifulSoup(tt_service, "html.parser")
tt_soup = str(tt_soup) # unicode(tt_soup) in Python 2
There might be a more appropriate method to call on the tt_soup
object instead of str()
, but I don't know because I'm not very familiar with BeautifulSoup.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.