[python][django學習篇][9]設計正在部落格視圖(3)

來源:互聯網
上載者:User

標籤:tom   cal   tle   樣式   成功   ionic   hit   rtc   frame   

需求: 真正的首頁視圖函數,當使用者訪問我們的部落格首頁時,他將看到我們發表的部落格文章列表,就像 示範項目 裡展示的這樣。t

https://docs.djangoproject.com/en/1.10/howto/static-files/

  1. 編寫部落格視圖函數
    • 從資料庫擷取文章的標題,將其作為模板變數傳遞到模板,所以視圖函數
      • # coding=utf8from django.shortcuts import renderfrom django.http import HttpResponsefrom .models import Post# Create your views here.def index(request):    post_list = Post.objects.all().order_by(‘-create_time‘)    return render(request, ‘blog/index.html‘, context={‘post_list‘: post_list})


        Post.objects.all()返回的是QuerySet,一個類似列表的對象,儲存所有文章。 order_by排序, -表示逆序表示先顯示最近編寫的文章(注意要修改index.html)

  2. 處理靜態檔案(使用網上模板:點我下載)
    • 靜態檔案的隱藏檔夾:應用blog目錄下static檔案夾,在static建立一個blog目錄,裡面有css 和js兩個檔案夾,拷貝網上模板代替html載入css 和javaScript檔案
    • 暫時使用網上模板的index.html代替原來的template/blog/index.html, 開啟網址可以看到首頁顯示非常混亂,這是因為沒有載入css檔案成功需要以 Django 的方式來正確地處理 CSS 和 JavaScript 等靜態檔案的載入路徑。CSS 樣式檔案通常在 HTML 文檔的 head 標籤裡引入
    • 開啟網上的index.html 中的<head>標籤如下:  
      • <!DOCTYPE html><html><head>    <title>Black &amp; White</title>    <!-- meta -->    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1">    <!-- css -->    <link rel="stylesheet" href="css/bootstrap.min.css">    <link rel="stylesheet" href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">    <link rel="stylesheet" href="css/pace.css">    <link rel="stylesheet" href="css/custom.css">    <!-- js -->    <script src="js/jquery-2.1.3.min.js"></script>    <script src="js/bootstrap.min.js"></script>    <script src="js/pace.min.js"></script>    <script src="js/modernizr.custom.js"></script></head>


        可以看到css檔案都在link 的href屬性裡面,可以看到諸如 `href="css/bootstrap.min.css" 或者 src="js/jquery-2.1.3.min.js" 這樣的引用,由於引用檔案的路徑不對,所以瀏覽器引入這些檔案失敗。我們需要把它們改成正確的路徑

      • 修改後的Html,
        templates/blog/index.html+ {% load staticfiles %}<!DOCTYPE html><html>  <head>      <title>Black &amp; White</title>      <!-- meta -->      <meta charset="UTF-8">      <meta name="viewport" content="width=device-width, initial-scale=1">      <!-- css -->      - <link rel="stylesheet" href="css/bootstrap.min.css">      <link rel="stylesheet" href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">      - <link rel="stylesheet" href="css/pace.css">      - <link rel="stylesheet" href="css/custom.css">      + <link rel="stylesheet" href="{% static ‘blog/css/bootstrap.min.css‘ %}">      + <link rel="stylesheet" href="{% static ‘blog/css/pace.css‘ %}">      + <link rel="stylesheet" href="{% static ‘blog/css/custom.css‘ %}">      <!-- js -->      - <script src="js/jquery-2.1.3.min.js"></script>      - <script src="js/bootstrap.min.js"></script>      - <script src="js/pace.min.js"></script>      - <script src="js/modernizr.custom.js"></script>      + <script src="{% static ‘blog/js/jquery-2.1.3.min.js‘ %}"></script>      + <script src="{% static ‘blog/js/bootstrap.min.js‘ %}"></script>      + <script src="{% static ‘blog/js/pace.min.js‘ %}"></script>      + <script src="{% static ‘blog/js/modernizr.custom.js‘ %}"></script>  </head>  <body>      <!-- 其它內容 -->      - <script src="js/script.js‘ %}"></script>      + <script src="{% static ‘blog/js/script.js‘ %}"></script>  </body></html>

        為了能在模板中使用 {% static %} 模板標籤,別忘了在最頂部 {% load staticfiles %} 。static 模板標籤位於 staticfiles 模組中,只有通過 load 模板標籤將該模組引入後,才能在模板中使用 {% static %} 標籤。

[python][django學習篇][9]設計正在部落格視圖(3)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.