2011-06-12 57 views
0

我通過djangobook網站學習Django,一切都很順利,但我創建的最新項目(處理模板)顯示了網頁中的所有html標記 - 我如何消除它們?Django呈現HTML標記 - 如何消除它們

views.py:

def current_datetime(request): 
    now = datetime.datetime.now() 
    return render_to_response('current_datetime.html',{'current_date':now}) 

base.html文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> 
<html lang="en"> 
<head> 
    <title>{% block title %}{% endblock %}</title> 
</head> 
<body> 
    <h1>My helpful timestamp site</h1> 
    {% block content %}{% endblock %} 
    {% block footer %} 
    <hr> 
    <p>Thanks for visiting my site.</p> 
    {% endblock %} 
</body> 
</html> 

current_datetime.html:

{% extends "base.html" %} 

{% block title %}The current time{% endblock %} 

{% block content %} 
<p>It is now {{ current_date }}.</p> 
{% endblock %} 

不幸的是,而不是它的顯示頁面,因爲它應該它用它顯示所有的HTML標籤。

+0

您可以嘗試螢火看看在響應頭文件中設置了什麼Content-Type。看起來它可能是text/plain而不是text/html。哦,它可能只是您的問題中的拼寫錯誤,但您的視圖在您將HTML文件列爲current_date.html時調用current_datetime.html。 – 2011-06-12 03:23:39

+0

'render_to_response()'應該默認創建一個'text/html'響應。 – 2011-06-12 03:28:48

+0

所以它顯示的東西類似於'當前時間<title>'而不是隻是'當前時間'? – <span class="text-secondary"> <small> <span>2011-06-12 03:56:39</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </div> <div class="answer-title"> <span class="text-logo margin-top-sm">A</span> <h2 class="title h4">回答</h2> </div> <div class="item-description text-md markdown-body margin-bottom-40 voidso"> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">0<i class="fa fa-thumbs-up"></i></span> </div> <div class="post-offset"> <div class="answer fmt"> <p>在你的urls.py</p> <pre><code class="prettyprint-override">from mysite.views import current_datetime urlpatterns = patterns('', (r'^time/$', current_datetime), ) </code></pre> <p>模板名稱必須等於鑑於命名current_datetime.html</p> 終端 <p>的runserver這樣</p> <pre><code class="prettyprint-override">$ python manage.py runserver </code></pre> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/6319951">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2011-06-12 03:30:07</span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> <!-- comments --> <div class="comments"> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">我不認爲這是問題。所有內容正在正確傳輸,它只是沒有正確格式。 (它仍然有html標籤) – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/324788/">startuprob</a></span> <span>2011-06-12 03:33:00</span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">檢查這個回購https://github.com/exezaid/mysite 是一個例子 – <span class="text-secondary"> <small> <span>2011-06-12 03:40:26</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="clearfix"> </div> <div class="relative-box"> <div class="relative">相關問題</div> <ul class="relative_list"> <li> 1. <a href="http://hk.uwenku.com/question/p-yapzgkej-ty.html" target="_blank" title="HTML標記未呈現"> HTML標記未呈現 </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-ktrkxbnn-bno.html" target="_blank" title="如何從ngModel呈現HTML標記?"> 如何從ngModel呈現HTML標記? </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-gocuivae-xw.html" target="_blank" title="StringBuilder呈現html標記"> StringBuilder呈現html標記 </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-nefeouxp-ph.html" target="_blank" title="Angular指令呈現HTML標記,我該如何阻止它?"> Angular指令呈現HTML標記,我該如何阻止它? </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-sdegsqkq-bdb.html" target="_blank" title="避免HTML和腳本標記呈現"> 避免HTML和腳本標記呈現 </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-qilxbeyg-rv.html" target="_blank" title="Mustachejs將html標記呈現爲html"> Mustachejs將html標記呈現爲html </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-vhcvanbw-gk.html" target="_blank" title="Django自定義標記未呈現(GAE)"> Django自定義標記未呈現(GAE) </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-adnmmonj-co.html" target="_blank" title="從AngularJS對象中呈現html標記"> 從AngularJS對象中呈現html標記 </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-myiefmhg-do.html" target="_blank" title="Django未使用python代碼分配標記來呈現html"> Django未使用python代碼分配標記來呈現html </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-tmzmgusy-t.html" target="_blank" title="呈現JavaScript對象內的HTML標記"> 呈現JavaScript對象內的HTML標記 </a> </li> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6208739752673518" data-ad-slot="4606349252"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <li> 11. <a href="http://hk.uwenku.com/question/p-xroixrxw-yb.html" target="_blank" title="從字符串呈現html標記"> 從字符串呈現html標記 </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-cyvwmyay-pe.html" target="_blank" title="django包含標記不呈現"> django包含標記不呈現 </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-ulzwvepb-kd.html" target="_blank" title="XSLT不會呈現HTML標記"> XSLT不會呈現HTML標記 </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-ocbutcla-bhu.html" target="_blank" title="如何呈現HTML Django頁面爲PDF?"> 如何呈現HTML Django頁面爲PDF? </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-aoswzpjf-bne.html" target="_blank" title="在C#中呈現HTML標記"> 在C#中呈現HTML標記 </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-kmtpgpny-bmy.html" target="_blank" title="JSF標記未呈現爲HTML"> JSF標記未呈現爲HTML </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-kgcmgkxu-or.html" target="_blank" title="如何解析django模板並呈現特定標記"> 如何解析django模板並呈現特定標記 </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-qxaprygt-bod.html" target="_blank" title="CDATA不呈現標記"> CDATA不呈現標記 </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-azjarwhj-bhp.html" target="_blank" title="如何呈現在預先標記HTML在ASP.NET MVC"> 如何呈現在預先標記HTML在ASP.NET MVC </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-omhbtjlo-hr.html" target="_blank" title="REACT如何使用React標記的庫插件來呈現HTML"> REACT如何使用React標記的庫插件來呈現HTML </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-mpfdaoih-nt.html" target="_blank" title="如何在HTML對象標記中呈現PDF字節數組"> 如何在HTML對象標記中呈現PDF字節數組 </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-udjxucad-bmh.html" target="_blank" title="asp.net - 如何呈現ListItem文本屬性中的HTML標記?"> asp.net - 如何呈現ListItem文本屬性中的HTML標記? </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-nbrabbil-uh.html" target="_blank" title="如何消除它們的地圖"> 如何消除它們的地圖 </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-pmeoxvpm-bp.html" target="_blank" title="SVG標記不呈現"> SVG標記不呈現 </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-qospcyvw-baq.html" target="_blank" title="c:foreach標記不呈現"> c:foreach標記不呈現 </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-vbusdlxj-qo.html" target="_blank" title="XSL:從HTML代碼中消除標記"> XSL:從HTML代碼中消除標記 </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-gtbrxxho-rt.html" target="_blank" title="從值中消除html標記"> 從值中消除html標記 </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-nxgolegq-bbc.html" target="_blank" title="Grails呈現標記和message.properties"> Grails呈現標記和message.properties </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-zybkqluc-ot.html" target="_blank" title="iOS使用超鏈接和圖像呈現HTML標記"> iOS使用超鏈接和圖像呈現HTML標記 </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-mverpxcp-ru.html" target="_blank" title="JSf標記未呈現"> JSf標記未呈現 </a> </li> </ul> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048261"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新問題 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-wszjnyuy-d.html" target="_blank" title="標籤變量循環"> 標籤變量循環 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-qyjrkuqo-cc.html" target="_blank" title="是不是將屬性設置爲等於有效的HTML值?"> 是不是將屬性設置爲等於有效的HTML值? </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-rcmymcwv-bmd.html" target="_blank" title="如何實現全局密鑰監聽器?"> 如何實現全局密鑰監聽器? </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-oluxqrwc-bkr.html" target="_blank" title="Laravel 5廣播事件到通配符通道使用節點和Laravel ECHO-服務器"> Laravel 5廣播事件到通配符通道使用節點和Laravel ECHO-服務器 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-pwfrxlsb-bdt.html" target="_blank" title="選擇默認類型要下載"> 選擇默認類型要下載 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-ncdnnkbo-bep.html" target="_blank" title="如何使用正確的列名將文本分隔爲列"> 如何使用正確的列名將文本分隔爲列 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-hctoaoej-beg.html" target="_blank" title="Inno Setup [設置] 64bit條件"> Inno Setup [設置] 64bit條件 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-mkycehfm-bgc.html" target="_blank" title="Linux上的Swift Objective-C互操作性錯誤:找不到'Foundation/Foundation.h'文件"> Linux上的Swift Objective-C互操作性錯誤:找不到'Foundation/Foundation.h'文件 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-tlgbplif-bev.html" target="_blank" title="的SyntaxError 255李斯特"> 的SyntaxError 255李斯特 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-ozjhreyu-bgs.html" target="_blank" title="如何在JAVA中使用apache POI在Excel中設置/取消設置列過濾器的值?"> 如何在JAVA中使用apache POI在Excel中設置/取消設置列過濾器的值? </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 相關問題</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-yapzgkej-ty.html" target="_blank" title="HTML標記未呈現"> HTML標記未呈現 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-ktrkxbnn-bno.html" target="_blank" title="如何從ngModel呈現HTML標記?"> 如何從ngModel呈現HTML標記? </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-gocuivae-xw.html" target="_blank" title="StringBuilder呈現html標記"> StringBuilder呈現html標記 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-nefeouxp-ph.html" target="_blank" title="Angular指令呈現HTML標記,我該如何阻止它?"> Angular指令呈現HTML標記,我該如何阻止它? </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-sdegsqkq-bdb.html" target="_blank" title="避免HTML和腳本標記呈現"> 避免HTML和腳本標記呈現 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-qilxbeyg-rv.html" target="_blank" title="Mustachejs將html標記呈現爲html"> Mustachejs將html標記呈現爲html </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-vhcvanbw-gk.html" target="_blank" title="Django自定義標記未呈現(GAE)"> Django自定義標記未呈現(GAE) </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-adnmmonj-co.html" target="_blank" title="從AngularJS對象中呈現html標記"> 從AngularJS對象中呈現html標記 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-myiefmhg-do.html" target="_blank" title="Django未使用python代碼分配標記來呈現html"> Django未使用python代碼分配標記來呈現html </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-tmzmgusy-t.html" target="_blank" title="呈現JavaScript對象內的HTML標記"> 呈現JavaScript對象內的HTML標記 </a> </li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://hk.uwenku.com/contact">聯系我們</a></li> <li>© 2020 HK.UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>