2015-04-02 142 views
0

在我的项目(基于Django的1.7)我有一个模板:如何重建django的模板?

views.py:

def help(request, page, anchor): 
    return render(request, 'help.html') 

help.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
    <head> 
     <TITLE>FinBox Help - {{ pagename }}</TITLE> 

     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

     <link rel="icon" href="media/pic/favicon.png" type="image/png"> 
     <link rel="shortcut icon" href="media/pic/favicon.png" type="image/png"> 
     <link rel="stylesheet" href="/media/css/docs.css" type="text/css"> 

     <script type="text/javascript"> 
      function tree_toggle(event) { 
       event = event || window.event 
       var clickedElem = event.target || event.srcElement 

       if (!hasClass(clickedElem, 'Expand')) { 
         return // click out the list 
       } 

       // Node, on which we've clicked 
       var node = clickedElem.parentNode 
       if (hasClass(node, 'ExpandLeaf')) { 
         return // click on the list 
       } 

       // define a new class for node 
       var newClass = hasClass(node, 'ExpandOpen') ? 'ExpandClosed' : 'ExpandOpen' 
       // replace the current class on newClass 
       // regexp finds isolated open|close and change it to newClass 
       var re = /(^|\s)(ExpandOpen|ExpandClosed)(\s|$)/ 
       node.className = node.className.replace(re, '$1'+newClass+'$3') 
      } 


      function hasClass(elem, className) { 
        return new RegExp("(^|\\s)"+className+"(\\s|$)").test(elem.className) 
      } 
     </script> 

     <style type="text/css"> 
     body 
     { 
      background-color: white; 
      background-image: none; 
      color: black; 
      font-family: Arial, Sans-Serif; 
      font-size: smaller; 
      font-style: normal; 
     } 

      A { 
      text-decoration: none; 
      } 
      A:hover { 
      text-decoration: underline; 
      color: red; 
      } 
     </style> 
    </head> 

    <body> 

     {% csrf_token %} 

     <div style="float:left; margin-left:310px;"> 
      Explanation how it works. 
     </div> 

     <div style="position:fixed; width:300px;"> 
      <div onclick="tree_toggle(arguments[0])"> 
       <ul class="Container"> 
        <li class="IsRoot"> 
         <div class="Expand"> 

         </div> 

         <div class="Content"> 
          Content 
         </div> 

         <ul class="Container"> 
          <li class="Node ExpandClosed"> 
           <div class="Expand"> 

           </div> 


           <div class="Content"> 
            <a href="/help/pur/"> 
             Destination 
            </a> 
           </div> 

           <ul class="Container"> 
            <li class="Node ExpandLeaf IsLast"> 
             <div class="Expand"> 

             </div> 

             <div class="Content"> 
              <a href="/help/test/"> 
               тест 
              </a> 
             </div> 
            </li> 
           </ul> 
          </li> 

          <li class="Node ExpandLeaf IsLast"> 
           <div class="Expand"> 

           </div> 

           <div class="Content"> 
            <a href="/help/errors/"> 
             System Exceptions 
            </a> 
           </div> 
          </li> 
         </ul> 
        </li> 
       </ul> 
      </div> 
     </div> 
    </body> 
</html> 

我不得不更换:

<ul class="Container"></ul> 

部分带有必须呈现目录结构的动态代码:

for root, dirs, files in os.walk(startpath): 
    print root, dirs, files 

如何更好地以django模板的方式执行它?我查看了文档,看起来我需要从头开始重新编写它,然后将其降低到基于类的模板。是对的还是不需要做? http://www.tangowithdjango.com/book/chapters/templates.html

谢谢!

回答

0

首先,您应该分开考虑想要显示在前端的项目以及如何在后端收集它们。让我们从后者开始。

看起来你正在把各种排序列表放在一起。如何准备这个列表并不重要,或者如果它甚至可以作为列表(你也可以传递对象,如果你喜欢的话 - 虽然我不会推荐在一般的练习中这样做,因为你可能会意外地传递给前端你不认为的东西),只要你这样做。此代码很可能会在您的应用程序/ views.py中执行

一旦拥有此列表,您需要将其传递给模板以供其呈现。通过传递方面,在下面叫mycontext的例子,你的模板,像这样做:

return render(request, 'help.html', context = mycontext) 

在模板中,你现在能叫出你通过上下文空前绝后。如果您愿意,您甚至可以通过此环境执行循环。有关动态模板的更多详细信息,请访问:https://docs.djangoproject.com/en/1.7/ref/templates/builtins/