認識 Django

哈哈被棄坑囉!

Django 簡介


Django 是一個基於 Python 語言所開發出的 Web 框架,跟之前介紹的 CodeIgniter 一樣目的是為了讓開發或是維護程式更方便,但相較於後者的 MVC 架構,Django 也有屬於它的 MTV 架構

官網連結:Django

Django 優點


  • 基於 Python 語言特性,套件安裝快速容易,且容易學習
  • 自帶後台管理系統 admin ,只需要通過簡單的幾行配置和程式碼就可以實現一個完整的後臺資料管理控制平臺。
  • debug 資訊詳盡,很容易找出程式碼錯誤所在。
  • 內建本地型輕量的 Web 伺服器,可以快速的開啟測試網站。

關於 MTV 及 MVC


在上網查了許多資料後主要有分為兩種說法

額外補充

Pylint


Pylint 介紹


Pylint 這個套件相當於 Python 版的 phpcs
如果不知道 phpcs 是甚麼的話 PHP_CodeSniffer
這邊簡單講解一下 Pylint 這套件主要是幫你找出不符合 Coding Style 的地方
像 PHP 依據的 Coding Style 是 PSR 而 Python 自己也有他的 Coding Style PEP
假如對為什麼要遵守 Coding Style 有疑問的話 什麼是Coding style?

Pylint 使用


首先要去安裝 Pylint 這套件
而 Python 安裝的方法就是去下 pip install pylint
因為這次我們有使用 Django 所以也要下 pip install pylint_django

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

下載好後要去更改 settings.json 的內容

{ "python.linting.enabled": true, "python.linting.pylintEnabled": true, "python.linting.pylintArgs": [ "--load-plugins=pylint_django", "--disable=django-not-configured", "--django-settings-module=主資料夾名稱(有 setting.py 的).settings", ],

Pylint 所控管的 Coding Style 中有需要對檔案、class、方法寫註解寫出他們的用處
如果想要關掉的話就加"--disable=C0114, C0115, C0116",在剛剛的 [ ] 裡面
(C0114=>檔案註解 C0115=>class註解 C0016=>方法註解)

成功的話有問題會顯示在 Prbblems Ctrl + Shift + M

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Django 2.1

環境設定


Python 安裝


Python 3.6.6

要記得 ☑ Add Python 3.6 to PATH

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

安裝完成之後,打開 VSCode 並叫出 Terminal ( 終端機 )
輸入 python version

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

VSCode 套件安裝


下載 VSCode 的擴充套件
Python 套件

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Django 套件

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Python 虛擬環境建置


接下來我們要建立虛擬環境目錄
一樣在 VSCode 的 Terminal 上先 cd 到你要創建的地方
輸入 mkdir 資料夾名稱創建好後 cd 進去

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

進到資料夾裡後輸入 python -m venv 虛擬環境名稱

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

先隨便創建個 .py 檔
接著重啟 VSCode 的 Terminal
然後他就會自動輸入指令讓你進入虛擬環境裡拉

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

想退出虛擬環境的話輸入 deactivate 就行囉
想要重新進入虛擬環境的話可以輸入 虛擬環境名稱\Scripts\activate

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

可能有些人在進入虛擬環境時會報錯
這時要用系統管理員的權限去開啟 PowerShell

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

開好後輸入 Set-ExecutionPolicy RemoteSigned 再輸入 y

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Django 2.1 安裝


在虛擬環境裡輸入 pip install "django<2.2"
輸入 django<2.2 可以確保我們安裝的是 2.1 的最新版本

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

確認安裝成功


第一種方法
接著輸入 python 進入進入互動式命令列環境
進入後輸入

import django django.VERSION

輸入完後會取得 Django 的版本訊息

要退出互動式命令列環境輸入 exit()

第二種方法
直接在 terminal 上輸入 django-admin version

建置 Django 第一個 project


在虛擬環境裡輸入 django-admin startproject 專案名稱

VSCode 基本環境設定


接著用 VSCode 開啟新創建得專案資料夾

打開 VSCode 的 settings ( 設定 ) 快捷鍵 ctrl + ,
在搜尋欄打上 Python
再把選項切換到 Workspace 上
再點選 Edit in settings.json

在 settings.json 加上

"python.defaultInterpreterPath": "虛擬環境路徑\\Scripts\\python.exe",

可以直接找到虛擬環境的目錄去複製
但要記得路徑的 \ 在 Window 的 VSCode 裡要用 \
看到下面狀態欄變 ( 虛擬目錄名稱 : venv ) 就成功了

建置 Django 第一個 app


這邊先說明 Django 的一個 app 就代表網站上的一個功能
所以通常一個網站都會有多個 app 去組成
那要創建一個 app 呢
在虛擬環境裡輸入 python manage.py startapp 功能名稱

Django settings 修改


創建好後打開 settings.py 找到 installed_app 裡面加剛剛我們創建的 app

修改 107 行以及 109 行改成 zh-Hant ( 中文繁體 ) 以及 'Asia/Taipei' ( 台北時區 )

簡單實作


歡迎介面

找到剛剛創建的 app 資料夾底下的 views.py 寫下下面的程式碼

from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse("歡迎來到 Django")

接著換到 urls.py 的檔案寫下下方的程式碼

from django.contrib import admin from django.urls import path from news import views urlpatterns = [ path('admin/', admin.site.urls), path('', views.index), ]

最後按下 f5 VSCode debug 運行檢查過沒 bug 就會自動啟動伺服器了

最後在網頁上輸入 http://127.0.0.1:8000/