python學習之Django(後續繼續補充)

DTL與HTML的區別

DTL作爲模板使用,是一種特殊語法既可以被django編譯的html文件,實現數據動態化。

渲染模板

  • reder

直接將模板傳遞進去,省去返回HttpResponse的步驟,使用時在根目錄的templates文件夾中創建html文件

from django.shortcuts import render
def index(request):
    return render(request,'index.html')
  • render_to_string
    需要在django.template.loader下導入,而且返回一個HttpResponse
from django.template.loader import render_to_string
from django.http import  HttpResponse

def index(request):
    html = render_to_string("index.html")
   return HttpResponse(html)

模板查找路徑

其實在setting中django已經將templates文件路徑配置
首先將當前項目的路徑動態獲取到BASE_DIR中。

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

在temolates中將templates路徑尋找到

TEMPLATES = [
  {
      'DIRS': [os.path.join(BASE_DIR, 'templates')]
    }
  • 使用reder渲染模板的時候,根目錄下的templates文件夾爲優先級即優先在DIRS中尋找
      'DIRS': [os.path.join(BASE_DIR, 'templates')]
  • 如果將DIRS中的路徑刪除
      'DIRS': []
  • 這個時候則會在 APP_DIRS中尋找,且優先在自己app中的templates的尋找,如果自己app中沒有找到則去別的app中尋找。(注意這裏所說的app必須在 INSTALLED_APPS表明)
  INSTALLED_APPS = [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'front'
]

DTL模板—變量

  • 使用ctrl+B跳轉到reder中發現該函數可以傳遞參數context
   context = {
      'username':'BingYiii'
  }
  return render(request,'index.html',context=context)
  • 這時只要在html中使用{{參數}}即可將context中的參數傳遞進去(注意context爲字典的格式)
{{ username }}

-可以向context字典的鍵值中傳入對象

class Person(object):
	def __init__(self,username):
	 	self.username = username 	
p = Person("BingYiii")
  context = {
     'person':p
 }
 return render(request,'index.html',context=context)

-這時可以在html中直接寫入方法

{{ person.username }}

DTL模板—標籤

  • if標籤
    所有的標籤都在{}之間實現,
    if標籤有閉合標籤endif
  {% if  %}
  # 使用pycharm在輸入if後按下Tab鍵會自動彈出兩行代碼
  {% endif %}
  • for…in標籤
    比python中for…in標籤多了empty語句。
 <ur> # 無序列表標籤
         {% for comment in comments %}
             <li>{{ comment }}</li> # li標籤是一個元素標籤,可以用在有序列表<ol>和無序列表<li>中
         {% empty %}
             <li>{{ '沒有評論' }}</li>
         {% endfor %}

 </ur>
   <table> # 表格標籤
       <thead> # 定義表格的表頭與<tbody>或者<tfoot>一同使用
           <tr> # 定義表格中的一行,單元格容器,每行可以容納多個單元格
               <td>序號</td> # td標籤和th標籤用來定義單元格,所有單元格都在tr標籤內,每個單元格由一對< td>和< /td>標籤或一對< th>和< /th>標籤表示,具體的表格內容放置在這一對td標籤或th標籤之中,
               <td>書名</td> 
               <td>作者</td>
               <td>價格</td>
           </tr>
       </thead>
       <tbody> # 對錶格的主題內容進行分組
       {% for book in books %}
           {% if forloop.first %}
               <tr style="background: red">
           {% elif forloop.last %}
               <tr style="background: blue">
           {% else %}
               <tr>
           {% endif %}
               <td>{{ forloop.counter0 }}</td>
               <td>{{ book.name }}</td>
               <td>{{ book.author }}</td>
               <td>{{ book.price }}</td>
           </tr>
       {% endfor %}
        </tbody>
   </table>
  • with標籤
    with標籤(將名稱替換)只能在{%with%}{%endwith%}中使用,不能在外部使用

         {% with persons.0 as zs %}
             <p>{{ zs }}</p>
             <p>{{ zs }}</p>
             <p>{{ zs }}</p>
         {% endwith %}
    
  • url標籤

  <style>
      .nav{
          overflow: hidden;
      }
      .nav li{
          float: left;
          list-style: none;
          margin: 0 20px;
      }
  </style>
<body>
  <ur class="nav">
          <li><a href="/">首頁</a></li>
          <li><a href="{% url 'book' %}">讀書</a></li>
          <li><a href={% url 'movie' %}>電影</a></li>
          <li><a href="{% url 'city' %}">同城</a></li>
          <li><a href="{% url 'detail' book_id=1 %}">最火的的博客</a></li>  # 傳遞多個參數,那麼通過空格的方式進行分隔
          <li><a href="{% url 'login' %}?next=/">登錄</a></li>
  </ur>
  • autoescape標籤
    DTL已經自動將字符進行轉義
    如果不需要轉義那麼輸入如下代碼:用來關掉自動轉義
  {% autoescape off %}
          {{info}}
  {% endautoescape %}
發佈了4 篇原創文章 · 獲贊 1 · 訪問量 484
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章