2017-10-11 62 views
0

私はDjangoテンプレートのHTMLテーブルとしてhtmlテーブルを持つ変数を渡そうとしています。Djangoテンプレートは、安全なまたはautoescapeを使用するかどうかにかかわらずhtmlテーブルを削除しています

私はそれを渡して|安全にするか、自動エスケープをオフにします。すべてのHTMLは挿入されていますが、テーブルは完全に削除されています。

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 

テンプレート

<div id="TALK-TALK-Service" style="width:50vw; height:50vw; float:left;"> 
     {{ TalkTalk |kksafe }} 
</div> 

シェル経由で印刷tt_data変数

<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 &amp; 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> 

HTMLはあなたのtt_soup変数があるWebページ

<div id="TALK-TALK-Service" style="width:50vw; height:50vw; float:left;"> 
     [&nbsp;, 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 &amp; 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 &amp; 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> 

答えて

2

上に表示bs4のオブジェクトで、文字列ではありません。これはあなたの視点で確認できます。

tt_soup = BeautifulSoup(tt_service, "html.parser") 
print(type(tt_soup)) 

tt_soupオブジェクトが呼び出し可能であるので、それは、テーブルをレンダリングする際に、Djangoテンプレート言語は、予期しない結果を与える、それを呼んでいます。これを防ぐには、tt_soupをビューの文字列に変換します。

tt_soup = BeautifulSoup(tt_service, "html.parser") 
tt_soup = str(tt_soup) # unicode(tt_soup) in Python 2 

ではなくstr()tt_soupオブジェクトを呼び出すために、より適切な方法があるかもしれませんが、私はBeautifulSoupに非常に精通していないよので、私は知りません。

+0

ありがとうございました – AlexW

関連する問題