iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 1
0
Modern Web

加速你的 Django 網站開發 - Django 的好用套件系列 第 1

01. Django

Django 是使用 Python 語言所開發的一個 Web 框架,採用了 MVT 的設計模式,也就是 Model - View - Template。簡單的說,Model 指的是與資料庫溝通的模型,View 指的是如何處理 HTTP 請求,Template 則是回應的 HTML 樣板。而這些就是為了完成 Django 的目標,要簡化以資料庫為基礎的網站開發。Django 非常強大,生態圈裡也有許多的套件被開發出來為 Django 助攻,讓 Django 有更多功能,開發更加便利。用 Django 也有四、五年了,這次參賽,就是想把之前用過的 Django 套件整理起來,讓以後的開發能更加快速。

那麼該怎麼開始使用 Django 來進行開發呢?在開始之前,先說明一下環境,我使用的環境是 Ubuntu 18.04 LTS ,而套件相依管理工具則是使用 poetry 。

Ubuntu 不用多說了,但 Poetry 怎麼好像,沒聽過?對,Poetry 是一個新的套件相依管理工具,在過去,Python 使用 setup.py, requirements.txt, setup.cfg, MANIFEST.in, Pipfile / Pipfile.lock 等檔案搭配 pip / pipenv 等工具來管理相依套件,Poetry 則是試著去解決掉這些麻煩,統一使用 pyproject.toml 來存放使用到的套件與套件版本。

安裝

由於 Ubuntu 的 Python 3 是 3.5,這邊要使用最新的 3.8,所以要安裝 pyenv

curl https://pyenv.run | bash

安裝好以後,關閉終端機,再重新開啟。

# 安裝 python 3.8 所需的套件
sudo apt-get install libbz2-dev libreadline-dev libsqlite3-dev python3-dev libpython3-dev
pyenv install 3.8.5

curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python3

# 依照官方文件是用 python,但因為環境是 Ubuntu 18.04,預設的 python 是 2.7
# 所以得用 python3,以確保 python 是 3.x 。
# 然後升級到 preview 版本,以確保後續的 poetry env 指令沒問題
poetry self update --preview

安裝以後,poetry 會被安裝到 $HOME/.poetry 下,使用時,需要把 $HOME/.poetry/bin 加到 PATH 環境變數裡,或者是用 source $HOME/.poetry/env 來確保 poetry 生效。

輸入 poetry -h 來看看有什麼指令吧

Poetry version 1.1.0b2

USAGE
  poetry [-h] [-q] [-v [<...>]] [-V] [--ansi] [--no-ansi] [-n] <command>
         [<arg1>] ... [<argN>]

ARGUMENTS
  <command>              The command to execute
  <arg>                  The arguments of the command

GLOBAL OPTIONS
  -h (--help)            Display this help message
  -q (--quiet)           Do not output any message
  -v (--verbose)         Increase the verbosity of messages: "-v" for normal
                         output, "-vv" for more verbose output and "-vvv" for
                         debug
  -V (--version)         Display this application version
  --ansi                 Force ANSI output
  --no-ansi              Disable ANSI output
  -n (--no-interaction)  Do not ask any interactive question

AVAILABLE COMMANDS
  about                  Shows information about Poetry.
  add                    Adds a new dependency to pyproject.toml.
  build                  Builds a package, as a tarball and a wheel by default.
  cache                  Interact with Poetry's cache
  check                  Checks the validity of the
                         pyproject.toml file.
  config                 Manages configuration settings.
  debug                  Debug various elements of Poetry.
  env                    Interact with Poetry's project environments.
  export                 Exports the lock file to alternative formats.
  help                   Display the manual of a command
  init                   Creates a basic pyproject.toml file in the
                         current directory.
  install                Installs the project dependencies.
  lock                   Locks the project dependencies.
  new                    Creates a new Python project at <path>.
  publish                Publishes a package to a remote repository.
  remove                 Removes a package from the project dependencies.
  run                    Runs a command in the appropriate environment.
  search                 Searches for packages on remote repositories.
  self                   Interact with Poetry directly.
  shell                  Spawns a shell within the virtual environment.
  show                   Shows information about packages.
  update                 Update the dependencies as according to the
                         pyproject.toml file.
  version                Shows the version of the project or bumps it when a
                         valid bump rule is provided.

看起來蠻多的,目前這篇只會用到 init / env / install / run 這幾個子命令。

開始建立專案

mkdir django-ithome-ironman
cd django-ithome-ironman
pyenv local 3.8.5
poetry init --no-interaction --dependency django
poetry env use $(pyenv which python3)
poetry install
poetry run django-admin.py startproject django_ithome_ironman .

pyenv local 3.8.5 表示當前的目錄要使用 Python 3.8.5,在當前的目錄也會找到一個 .python-version 的檔案,裏面就是 3.8.5。

poetry init 表示在目前的資料夾進行初始化,no-interaction 表示不問問題,dependency 則表示預先加入 Django 套件

poetry env use 表示建立一個 python3 的虛擬環境,基本上跟 virtualenv / python -m venv / pyenv 相似,就只是確保有一個乾淨的 python 執行環境。

poetry install 則是表示依照 pyproject.toml 裡的描述來安裝套件。

poetry run 是表示以虛擬環境裡的 python 來執行指令,這邊是執行 django 所提供的 django-admin.py 來在目前的資料夾裡建立專案。

建立好了以後,就可以啟動網站看看結果啦~

poetry run python manage.py migrate
poetry run python manage.py runserver

參考資料:


下一篇
02. django-environ
系列文
加速你的 Django 網站開發 - Django 的好用套件30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言