時間:2023-06-09 19:48:02 | 來源:網(wǎng)站運營
時間:2023-06-09 19:48:02 來源:網(wǎng)站運營
Django搭建個人博客: Bootstrap 4 改寫模板:注意:學本章之前請檢查 Django 版本,確保安裝的是 Django 2 而不是 Django 3,否則后面所有的章節(jié)都會遇到staticfiles
無法載入的錯誤。在虛擬環(huán)境中輸入上一章我們的網(wǎng)站頁面實在太粗糙,你肯定不會拿來做真正的博客首頁。因此這章我們要借助Bootstrap的力量,改寫一個大氣的博客。pip list
即可查看。
static/bootstrap/
,用于存放Bootstrap靜態(tài)文件。靜態(tài)文件通常指那些不會改變的文件。Bootstrap中的css、js文件,就是靜態(tài)文件。css
和js
兩個文件夾復制進去。2020.07.22 更新:現(xiàn)在我們的
由于popper.js
版本兼容問題,教程改用 cdn 遠程引入的形式,所以這里就不需要將 popper.js 下載到本地了。后面會講到如何 cdn 引入。
static/
目錄結構像這樣:my_blog│├─article└─my_blog│ ...└─static └─bootstrap │ ├─css # 文件夾 │ └─js # 文件夾 └─jquery │ └─jquery-3.3.1.js # 文件 └─popper └─popper-1.14.4.js # 文件 - 2020.07.22: 不用下載
因為在Django中需要指定靜態(tài)文件的存放位置,才能夠在模板中正確引用它們。因此在settings.py
的末尾加上:my_blog/settings.py...STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static"),)
再確認一下settings.py
中有沒有STATIC_URL = '/static/'
字段,如果沒有把它也加在后面。templates/
中,新建三個文件:base.html
是整個項目的模板基礎,所有的網(wǎng)頁都從它繼承;header.html
是網(wǎng)頁頂部的導航欄;footer.html
是網(wǎng)頁底部的注腳。templates/
的結構像下面這個樣子:templates│├─base.html├─header.html├─footer.html└─article └─list.html # 上一章創(chuàng)建的
加上之前的list.html
,接下來就要重新寫這4個文件了。Bootstrap是非常優(yōu)秀的前端框架,上手簡單,所以很流行。你可以在官方網(wǎng)站上進行系統(tǒng)的學習。通篇看Bootstrap文檔比較枯燥,因此建議你可以像查字典一樣,需要用哪個模塊,就到官網(wǎng)上找相關的代碼,修改一下拷貝到你的項目中。這里會一次性寫大量代碼,不要著急慢慢看,理解了就很簡單了。
base.html
:templates/base.html<!-- 載入靜態(tài)文件 使用 Django 3 的讀者改為 {% load static %}-->{% load staticfiles %}<!DOCTYPE html><!-- 網(wǎng)站主語言 --><html lang="zh-cn"><head> <!-- 網(wǎng)站采用的字符編碼 --> <meta charset="utf-8"> <!-- 預留網(wǎng)站標題的位置 --> <title>{% block title %}{% endblock %}</title> <!-- 引入bootstrap的css文件 --> <link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.min.css' %}"></head><body> <!-- 引入導航欄 --> {% include 'header.html' %} <!-- 預留具體頁面的位置 --> {% block content %}{% endblock content %} <!-- 引入注腳 --> {% include 'footer.html' %} <!-- bootstrap.js 依賴 jquery.js 和popper.js,因此在這里引入 --> <script src="{% static 'jquery/jquery-3.3.1.js' %}"></script> <!-- popper.js 采用 cdn 遠程引入,意思是你不需要把它下載到本地。 在實際的開發(fā)中推薦靜態(tài)文件盡量都使用 cdn 的形式。 教程采用本地引入是為了讓讀者了解靜態(tài)文件本地部署的流程。 --> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1-lts/dist/umd/popper.min.js"></script> <!-- 引入bootstrap的js文件 --> <script src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script></body></html>
{% load staticfiles %}
之后,才可使用 {% static 'path' %}
引用靜態(tài)文件。<head></head>
標簽內包含網(wǎng)頁的元數(shù)據(jù),是不會在頁面內顯示出來的。<body></body>
標簽內才是網(wǎng)頁會顯示的內容。header.html
:templates/header.html<!-- 定義導航欄 --><nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container"> <!-- 導航欄商標 --> <a class="navbar-brand" href="#">我的博客</a> <!-- 導航入口 --> <div> <ul class="navbar-nav"> <!-- 條目 --> <li class="nav-item"> <a class="nav-link" href="#">文章</a> </li> </ul> </div> </div></nav>
標簽內的class
屬性是Bootstrap樣式的定義方法。試著改寫或刪除其中一些內容,觀察頁面的變化。list.html
:templates/article/list.html<!-- extends表明此頁面繼承自 base.html 文件 -->{% extends "base.html" %}{% load staticfiles %}<!-- 寫入 base.html 中定義的 title -->{% block title %} 首頁{% endblock title %}<!-- 寫入 base.html 中定義的 content -->{% block content %}<!-- 定義放置文章標題的div容器 --><div class="container"> <div class="row mt-2"> {% for article in articles %} <!-- 文章內容 --> <div class="col-4 mb-4"> <!-- 卡片容器 --> <div class="card h-100"> <!-- 標題 --> <h4 class="card-header">{{ article.title }}</h4> <!-- 摘要 --> <div class="card-body"> <p class="card-text">{{ article.body|slice:'100' }}...</p> </div> <!-- 注腳 --> <div class="card-footer"> <a href="#" class="btn btn-primary">閱讀本文</a> </div> </div> </div> {% endfor %} </div></div>{% endblock content %}
{% block title %}
和{% block content %}
是如何與base.html
中相對應起來的。{{ article.body|slice:'100' }}
取出了文章的正文;其中的|slice:'100'
是Django的過濾器語法,表示取出正文的前100個字符,避免摘要太長。footer.html
:{% load staticfiles %}<!-- Footer --><div> <br><br><br></div><footer class="py-3 bg-dark fixed-bottom"> <div class="container"> <p class="m-0 text-center text-white">Copyright © www.dusaiphoto.com 2018</p> </div></footer>
呼,真是一大堆的東西啊。url
訪問list.html
時,頂部的{% extends "base.html" %}
告訴Django:“這個文件是繼承base.html
的,你去調用它吧。”base.html
文件:{% include 'header.html' %}
表明這里需要加入header.html
的內容{% include 'footer.html' %}
加入footer.html
的內容{% block content %}{% endblock content %}
表明這里應該加入list.html
中的對應塊的內容http://127.0.0.1:8000/article/article-list/
,看到如下頁面:關鍵詞:改寫,模板
微信公眾號
版權所有? 億企邦 1997-2025 保留一切法律許可權利。