Django -- Templates

來源:互聯網
上載者:User

標籤:

 安裝Django時會自動安裝Jinja2,用於模板渲染.

 

static、templates在project中的位置:appname - templates/static - appname

使用時只需像這樣:"blog/blog.html"、"blog/css/bootstrap.min.css"、"blog/img/bg.jpg",Django會自動到各自的目錄中去尋找相應的檔案

每個app都保持這種結構,可以避免名字衝突

編寫blog/views.py 

from django.shortcuts import renderdef blog(request):      #第一個參數必須有(一般叫request)    return render(request, ‘blog/blog.html‘)    

 

 

Jinja基本文法執行個體

<!-basic.html->

<DOCTYPE html>
<html lang="en">
<head>
  {% block title %} <!-可在子類中替換的區塊->
  <title>Homepage</title>
  <% endblock %>
  <meta charset="UTF-8">
  {% load staticfiles %} <!-載入和使用靜態檔案,也可以直接 href=‘blog/css/bootstrap.min.css‘ ->
  <link rel="stylesheet" type="text/css" href="{% static ‘blog/css/bootstrap.min.css‘ %}">
</head>
<body>
  {% block content %}       <!-可在子類中替換的區塊->
  {% endblock %}

</body>
</html>



<!-blog.html->
{% extends "blog/basic.html" %} <!-繼承basic.html模板中的所有內容->

{% block title%}            <!-替換父類模板中title區塊的內容->
  <title>My Blog</title>
{% endblock %}

{% block content %}
   
  {% for post in object_list %} <!-for、if的使用->
    {% if {{ post.name} == ‘zoro‘ %} <!-變數的使用->
      <p>{{ post.date|date:"Y-m-d" }}</p> <!-使用過濾->
    {% endif %}
  {% endfor %} 
  
{% endblock %}

 

Django -- Templates

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.