開始使用 Django
根據你使用 Django 的經驗程度,你可以嘗試教學或直接深入了解文件。
想深入了解 Django 嗎?閱讀概覽,查看 Django 是否符合你的專案需求。
Django 概覽撰寫你的第一個 Django 應用程式
已經安裝 Django 了嗎?很好。現在嘗試這個教學,將會帶你建立一個基本的民意調查應用程式。它分為兩個部分
- 公開網站,讓人們查看民意調查並參與投票。
- 管理介面,讓你新增、變更和刪除民意調查。
Django 簡介
-
物件關聯性映射
完全用 Python 定義你的資料模型。你可以免費獲得豐富且動態的資料庫存取 API,但若有需要,你仍然可以撰寫 SQL。
進一步閱讀from django.db import models class Band(models.Model): """A model of a rock band.""" name = models.CharField(max_length=200) can_rock = models.BooleanField(default=True) class Member(models.Model): """A model of a rock band member.""" name = models.CharField("Member's name", max_length=200) instrument = models.CharField( choices=( ("g", "Guitar"), ("b", "Bass"), ("d", "Drums"), ), max_length=1, ) band = models.ForeignKey("Band")
-
{% translate "網址和檢視
乾淨俐落的網址配置是高品質網路應用程式中的重要細節。Django 鼓勵打造漂亮的網址,而且不會在網址中加入任何不必要的資料,例如 .php 或 .asp。
要為應用程式設計網址,你必須建立名為 URLconf 的 Python 模組。它就像應用程式的目錄,其中包含 URL 模式和檢視之間的簡單對應。
進一步閱讀from django.urls import path from . import views urlpatterns = [ path("bands/", views.band_listing, name="band-list"), path("bands/<int:band_id>/", views.band_detail, name="band-detail"), path("bands/search/", views.band_search, name="band-search"), ]
from bands.models import Band from django.shortcuts import render def band_listing(request): """A view of all bands.""" bands = Band.objects.all() return render(request, "bands/band_listing.html", {"bands": bands})
-
範本
Django 的範本語言旨在取得功能和簡便性之間的平衡。它的設計初衷是讓習慣使用 HTML 的人(例如設計師和前端開發人員)感到舒適且易於學習。但它也具備彈性和高度的擴充性,開發人員可以根據需要擴充範本語言。
進一步閱讀<html> <head> <title>{% translate "Band Listing" %}</title> </head> <body> <h1>{% translate "All Bands" %}</h1> <ul> {% for band in bands %} <li> <h2><a href="{{ band.get_absolute_url }}">{{ band.name }}</a></h2> {% if band.can_rock %}<p>{% translate "This band can rock!" %}</p>{% endif %} </li> {% endfor %} </ul> </body> </html>
-
表單
Django 提供強大的表單函式庫,可以將表單呈現為 HTML、驗證使用者提交的資料,以及將資料轉換為原生 Python 類型。Django 還提供將現有的模型產生表單的方法,並使用這些表單建立和更新資料。
進一步閱讀from django import forms class BandContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.TextField() sender = forms.EmailField() cc_myself = forms.BooleanField(required=False)
-
驗證
Django 具有一套安全且功能完備的驗證系統。它負責處理使用者帳戶、群組、權限和基於 cookie 的使用者工作階段。這樣,你可以輕鬆地建立允許使用者建立帳戶並安全登入/登出的網站。
進一步閱讀from django.contrib.auth.decorators import login_required from django.shortcuts import render @login_required def my_protected_view(request): """A view that can only be accessed by logged-in users""" return render(request, "protected.html", {"current_user": request.user})
-
管理員
Django 最強大的部分之一就是它的自動管理介面。它會讀取模型中的元資料,以提供功能強大且生產就緒的介面,讓內容製作者可以立即使用此介面開始管理網站上的內容。它的設定很簡單,而且提供許多自訂程式碼用的掛鉤。
進一步閱讀from bands.models import Band, Member from django.contrib import admin class MemberAdmin(admin.ModelAdmin): """Customize the look of the auto-generated admin for the Member model""" list_display = ("name", "instrument") list_filter = ("band",) admin.site.register(Band) # Use the default options admin.site.register(Member, MemberAdmin) # Use the customized options
-
國際化
Django 提供完整支援,將文字翻譯成各種語言,並針對特定語言環境格式化日期、時間、數字和時區。透過此支援,開發人員和範本作者能夠指定應用程式中的哪些部分會針對當地語言和文化進行翻譯或格式化,並使用這些掛勾依據使用者偏好,為特定使用者將網路應用程式在地化。
進一步閱讀from django.shortcuts import render from django.utils.translation import gettext def homepage(request): """ Shows the homepage with a welcome message that is translated in the user's language. """ message = gettext("Welcome to our site!") return render(request, "homepage.html", {"message": message})
{% load i18n %} <html> <head> <title>{% translate "Homepage - Hall of Fame" %}</title> </head> <body> {# Translated in the view: #} <h1>{{ message }}</h1> <p> {% blocktranslate count member_count=bands.count %} Here is the only band in the hall of fame: {% plural %} Here are all the {{ member_count }} bands in the hall of fame: {% endblocktranslate %} </p> <ul> {% for band in bands %} <li> <h2><a href="{{ band.get_absolute_url }}">{{ band.name }}</a></h2> {% if band.can_rock %}<p>{% translate 'This band can rock!' %}</p>{% endif %} </li> {% endfor %} </ul> </body> </html>
-
安全性