from Django official document;
Django 最初被設計用於具有快速開發需求的新聞類站點,目的是要實現簡單快捷的網站開發。
from 編寫你的第一個 Django 應用,第 1 部分.
the first Django startProject
1. create our project "mysite"
讓我們看看 startproject
創建了些什么:
mysite/ manage.py mysite/ __init__.py settings.py urls.py asgi.py wsgi.py
這些目錄和文件的用處是:
- The outer
mysite/
root directory is a container for your project. Its name doesn't matter to Django; you can rename it to anything you like. manage.py
: 一個讓你用各種方式管理 Django 項目的命令行工具。你可以閱讀 django-admin and manage.py 獲取所有manage.py
的細節。- 里面一層的
mysite/
目錄包含你的項目,它是一個純 Python 包。它的名字就是當你引用它內部任何東西時需要用到的 Python 包名。 (比如mysite.urls
). mysite/__init__.py
:一個空文件,告訴 Python 這個目錄應該被認為是一個 Python 包。mysite/settings.py
:Django 項目的配置文件。如果你想知道這個文件是如何工作的,請查看 Django settings 。mysite/urls.py
:Django 項目的 URL 聲明,就像你網站的“目錄”。閱讀 URL dispatcher 文檔來獲取更多關於 URL 的內容。mysite/asgi.py
: An entry-point for ASGI-compatible web servers to serve your project. See How to deploy with ASGI for more details.mysite/wsgi.py
:作為你的項目的運行在 WSGI 兼容的Web服務器上的入口。閱讀 如何使用 WSGI 進行部署 了解更多細節。
Django 自帶的用於開發的簡易服務器,它是一個用純 Python 寫的輕量級的 Web 服務器。我們將這個服務器內置在 Django 中是為了讓你能快速的開發出想要的東西,因為你不需要進行配置生產級別的服務器(比如 Apache)方面的工作,除非你已經准備好投入生產環境了。
現在是個提醒你的好時機:千萬不要 將這個服務器用於和生產環境相關的任何地方。這個服務器只是為了開發而設計的。(我們在 Web 框架方面是專家,在 Web 服務器方面並不是。)
2. create your app
Projects vs. apps
What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a small poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.
In this tutorial, we’ll create our "poll" app right next to your manage.py
file so that it can be imported as its own top-level module.
That’ll create a directory polls
, which is laid out like this:
polls/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py
在mysite/setting.py里, INSTALLED_APPS里面添加新增的應用.
3. create your first view
可以在app/目錄下添加urls.py,然后使用include()函數將app/urls.py添加到global 的urls.py中.
from django.contrib import admin from django.urls import include, path urlpatterns = [ path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ]
我們設計 include()
的理念是使其可以即插即用。因為投票應用有它自己的 URLconf( polls/urls.py
),他們能夠被放在 "/polls/" , "/fun_polls/" ,"/content/polls/",或者其他任何路徑下,這個應用都能夠正常工作。
4. configure your database
from 編寫你的第一個 Django 應用,第 2 部分.
現在,打開 mysite/settings.py
。這是個包含了 Django 項目設置的 Python 模塊。通常,這個配置文件使用 SQLite 作為默認數據庫。
如果你想使用其他數據庫,你需要安裝合適的 database bindings ,然后改變設置文件中 DATABASES
'default'
項目中的一些鍵值:
ENGINE
-- 可選值有'django.db.backends.sqlite3'
,'django.db.backends.postgresql'
,'django.db.backends.mysql'
,或'django.db.backends.oracle'
。其它 可用后端。NAME
- 數據庫的名稱。如果使用的是 SQLite,數據庫將是你電腦上的一個文件,在這種情況下,NAME
應該是此文件的絕對路徑,包括文件名。默認值os.path.join(BASE_DIR, 'db.sqlite3')
將會把數據庫文件儲存在項目的根目錄。
如果你不使用 SQLite,則必須添加一些額外設置,比如 USER
、 PASSWORD
、 HOST
等等。想了解更多數據庫設置方面的內容,請看文檔:DATABASES
。
$ python manage.py migrate
The migrate
command looks at the INSTALLED_APPS
setting and creates any necessary database tables according to the database settings in your mysite/settings.py
file and the database migrations shipped with the app (we'll cover those later). You'll see a message for each migration it applies. If you're interested, run the command-line client for your database and type \dt
(PostgreSQL), SHOW TABLES;
(MariaDB, MySQL), .schema
(SQLite), or SELECT TABLE_NAME FROM USER_TABLES;
(Oracle) to display the tables Django created.
5. create your model
在 Django 里寫一個數據庫驅動的 Web 應用的第一步是定義模型 - 也就是數據庫結構設計和附加的其它元數據。