起因
這2天更新了個(gè)人博客網(wǎng)站的代碼瘦真,基本把大部分功能做完了花枫。剛剛更新完后,打開(kāi)博客首頁(yè)一看網(wǎng)頁(yè)上有幾個(gè)奇怪的小方塊尚困,在博客正文里卻沒(méi)有了蠢箩。查看網(wǎng)頁(yè)源碼,多了好幾個(gè)<code> </code>
這是什么事甜?我記得我沒(méi)在頁(yè)面中寫(xiě)過(guò)谬泌,趕緊回去看源碼發(fā)現(xiàn)源碼中沒(méi)有寫(xiě)過(guò)這個(gè)啊,再開(kāi)自己的runserver
調(diào)試逻谦,發(fā)現(xiàn)在自己的電腦上就是正常的掌实。難道在部署的機(jī)器上有問(wèn)題?不會(huì)啊邦马,沒(méi)更新代碼之前沒(méi)有這個(gè)問(wèn)題凹恰!
過(guò)程
檢查了很久才發(fā)現(xiàn)滋将,原來(lái)我用markdown
顯示前端頁(yè)面代碼邻悬,makedown
會(huì)在前端用<code> import django </code>
來(lái)顯示代碼。在首頁(yè)展示博客正文時(shí)随闽,用django template tags
的truncatechars
來(lái)只顯示博客文章的前200個(gè)字符父丰,結(jié)果truncatechars
把html代碼截?cái)嗔耍挥星耙粋€(gè)<code>
掘宪,沒(méi)有</code>
蛾扇,所以導(dǎo)致前端頁(yè)面的不正常顯示∥汗觯可是在我自己的電腦上卻沒(méi)有這個(gè)問(wèn)題镀首,看來(lái)原因就是開(kāi)發(fā)環(huán)境和生產(chǎn)環(huán)境的django
版本不統(tǒng)一。
結(jié)果
最后查看了最新的文檔更換使用truncatewords_html
更好
truncatewords_html
Similar to truncatewords, except that it is aware of HTML tags. Any tags that are opened in the string and not closed before the truncation point, are closed immediately after the truncation.
This is less efficient than truncatewords, so should only be used when it is being passed HTML text.
For example:
{{ value|truncatewords_html:2 }}
If value is <p>Joel is a slug</p>
, the output will be <p>Joel is ...</p>
.
Newlines in the HTML content will be preserved
后記
此事得出的教訓(xùn)及經(jīng)驗(yàn):
- 需要對(duì)開(kāi)發(fā)環(huán)境和生產(chǎn)環(huán)境統(tǒng)一性的檢查
- 文檔看的不仔細(xì)后果很?chē)?yán)重啊