標籤:tom cal tle 樣式 成功 ionic hit rtc frame
需求: 真正的首頁視圖函數,當使用者訪問我們的部落格首頁時,他將看到我們發表的部落格文章列表,就像 示範項目 裡展示的這樣。t
https://docs.djangoproject.com/en/1.10/howto/static-files/
- 編寫部落格視圖函數
- 從資料庫擷取文章的標題,將其作為模板變數傳遞到模板,所以視圖函數
# 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)
- 處理靜態檔案(使用網上模板:點我下載)
- 靜態檔案的隱藏檔夾:應用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 & 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 & 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)