iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 16
2
Modern Web

🍐放學後的網頁開發系列 第 16

[Day16] 柚子放學後的網頁生活 - Django & Bootstrap

之前有用Template建立過whoami.html,今天要接著分享囉 ~

Bootstrap

今天要來介紹其他模板來幫助我們後面專案執行 - Bootstrap,一種網站樣板

如果由網站設計者的語言來說,可以解釋為它是css 檔、一些圖片跟一些 JS組成的,我們可以從 Bootstrap 網站看到下載點、各類文件跟說明,並運用在我們的網站上。

Bootrap:https://getbootstrap.com/

Get Start

我們來到Bootrap首頁後,直接看 Getting-start 頁面
裡面有兩個很重要的部分,分別是我們要import的CSS和JS(非必要)
CSS:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

JS:

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

習慣上,我都直接放進網頁內,反正這邊多寫也不會有影響 XD

那我們就可以先準備好一個 index.html 做為後面專案的首頁,檔案位置就跟之前的一樣,放在templates底下

接著我們回到 Start template 的部分把整段寫入新建好的 index.html

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
    <h1>Hello, world!</h1>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
  </body>
</html>

成功貼上後,就要來複習之前怎麼設定Template了

View

還記得上次怎麼render嗎?
這次我們還沒有要帶任何變數過去,所以

def index(request):
    return render(request, 'index.html', {
    })

先把括號開好,但還不用寫沒關係
或是想拿掉也可

def index(request):
    return render(request, 'index.html')

URL

確認View寫好後,我們現在還沒有給他url,那就沒辦法瀏覽呀
所以我們在urlpatterns底下,加上

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^hellonuts/$', hello_world),
    url(r'^index/',index)
]

之後我們只要連到localhost:8000/index
它就會幫我們呼叫剛剛在view寫的 index function
接著 index function 會幫我們 render index.html
最後就會顯示 index.html 在我們眼前囉

學會套用Bootstrap後,現在來換上這次要用的模板 - Jumbotron

Jumbotron

先來到 examples 的頁面,裡面有非常多範例你可以好好玩,這次我選的是右邊這個

因為我覺得它的彈性很大,要調整比較容易,如果你想用其他的玩也沒關係哦
如果沒找到的
Jumbotron:https://getbootstrap.com/docs/4.0/components/jumbotron/

點進去後,

咦,沒有code阿,那這次我要怎麼放到我的專案
右鍵->檢視網頁原始碼

現在要做的動作你就知道了吧! 我們抓body的部分就好
接著再回到我們的網站看是不是跟Example網頁上一樣囉

環境準備好後,下節會講些常用的小操作

下課囉 ~ 請鎖定 柚子放學後的網頁生活


上一篇
[Day15] 柚子放學後的網頁生活 - Django 會員系統 (admin)
下一篇
[Day17] 柚子放學後的網頁生活 - Django Static Resource
系列文
🍐放學後的網頁開發30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言