Django 建立一個 app 要做的事情


建立時間: 2023年8月14日 01:00
更新時間: 2023年8月14日 17:42

說明

本篇將分享建立一個 app 要做哪些設定。

以下範例都是參考 Django 官方教學,非常具有參考價值。

建立 app

首先在終端切換到 Django 專案,使用 Django 指令建立 app,your_app 換成你想要的 app 名稱。

$ python manage.py startapp your_app

我將以 official app 作為範例

$ python manage.py startapp official

以下是 official app 資料夾結構

official
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   ├── __init__.py
├── models.py
├── tests.py
└── views.py

激活模型

模型是 Django 用來處理資料庫的功能,你可以在 official/models.py 建立模型,記得要在 INSTALLED_APPS 添加 "official.apps.OfficialConfig",模型水很深,所以這裡不會細講模型。

my_site/settings.py

INSTALLED_APPS = [
    "official.apps.OfficialConfig",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
]

之後你可以這樣 $ python manage.py makemigrations official 儲存遷移,當然 $ python manage.py makemigrations 會自動偵測所有的模型,意思是建立資料庫修改。

視圖

平常怎麼寫視圖就直接在就行了,沒有太大的變化,主要是模板的位置。

official/views.py

from django.shortcuts import render


def index(request):
    context = {}

    return render(request, "official/index.html", context)

靜態文件

靜態文件指的是 css, javascript, image 等等的資料,存放位置在 official/static/ 底下。

資料夾結構如下。

official
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   ├── __init__.py
├── models.py
├── static
│   └── official
│       ├── css
│       │   └── style.css
│       └── images
│           └── background.jpg
├── tests.py
└── views.py

我建立一個 style.css 和 background.jpg 圖片,程式碼如下。

official/static/official/css/style.css

body {
  background: white url("../images/background.jpg") no-repeat;
}

h1 {
  color: red;
}

我在 style.css 存取圖片,相對路徑以檔案當前的位置為基底,images 資料夾和 css 資料夾同一層,所以相對路徑為 ../images

模板

模板要在 official 資料夾底下建立 templates,然後在 templates 再建立一個 official 資料夾,裡面再存放 html 檔。

所以 official 的 index.html 要存放在 official/templates/official/index.html

資料夾結構如下。

official
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   ├── __init__.py
├── models.py
├── static
│   └── official
│       ├── css
│       │   └── style.css
│       └── images
│           └── background.jpg
├── templates
│   └── official
│       └── index.html
├── tests.py
└── views.py

templates 再建立一個 app 名稱的資料夾是 Django 建議的,資料夾名稱類似命名空間,如果每個 app 都在 templates 底下建立 index.html,這樣 Django 會無法辨識要讀取哪個 app 的 index.html。

index.html 程式碼如下。

official/templates/official/index.html

<!DOCTYPE html>
<html lang="zh-Hant-TW">
  <head>
    <meta charset="utf-8">
    <meta name="description" content="description">
    <meta name="keywords" content="keywords">
    <title>Page Title</title>
    {% load static %}
    <link rel="stylesheet" href="{% static 'official/css/style.css' %}">
  </head>
  <body>
    <h1>This is a Heading</h1>
    <p>This is a paragraph.</p>
  </body>
</html>

建立 urls.py

在 official 資料夾底下建立 urls.py

以下是 official app 資料夾結構

official
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   ├── __init__.py
├── models.py
├── static
│   └── official
│       ├── css
│       │   └── style.css
│       └── images
│           └── background.jpg
├── templates
│   └── official
│       └── index.html
├── tests.py
├── urls.py
└── views.py

範例

official/urls.py

from django.urls import path

from . import views

# 命名空間
# template url 命名空間使用範例
# {% url 'official:index' %}
app_name = "official"
urlpatterns = [
    path("", views.index, name="index"),
]

app_name 是 url 的命名空間,因為可能有多個 app 都用同樣的名稱,像是 index,加上命名空間可以避免衝突。

在專案底下的 urls.py 加上 official 的 urls,假設專案名稱是 my_site

my_site/urls.py

from django.contrib import admin
from django.urls import include
from django.urls import path

urlpatterns = [
    path("official/", include("official.urls")),
    path("admin/", admin.site.urls),
]

admin 是本來就存在的可以先忽略,path("official/", include("official.urls")) 代表著所有的 official urls 路徑都會在 official 底下,這樣以後只需在 official/urls.py 添加 url 就行了。

參考

觀看次數: 463
appdjangostarapp
按讚追蹤 Enjoy 軟體 Facebook 粉絲專頁
每週分享資訊技術

一杯咖啡的力量,勝過千言萬語的感謝。

支持我一杯咖啡,讓我繼續創作優質內容,與您分享更多知識與樂趣!