Python 版本
Python 3.5.2
安裝 virtualenv
c5220056@GMPTIC:~/myworkplace$ virtualenv ll_env --python=python3
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/c5220056/myworkplace/ll_env/bin/python3
Also creating executable in /home/c5220056/myworkplace/ll_env/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
激活虛擬環(huán)境
c5220056@GMPTIC:~/myworkplace$ source ll_env/bin/activate
(ll_env) c5220056@GMPTIC:~/myworkplace$
安裝 Django
(ll_env) c5220056@GMPTIC:~/myworkplace$ pip3 install Django
Collecting Django
Using cached Django-2.0.1-py3-none-any.whl
Collecting pytz (from Django)
Using cached pytz-2017.3-py2.py3-none-any.whl
Installing collected packages: pytz, Django
Successfully installed Django-2.0.1 pytz-2017.3
在 Django 中創(chuàng)建項目
在處于活動的虛擬環(huán)境下(ll_env包含在括號內(nèi)),執(zhí)行如下命令來新建一個項目:
(ll_env) c5220056@GMPTIC:~/myworkplace$ django-admin.py startproject learning_log .
(ll_env) c5220056@GMPTIC:~/myworkplace$ ls
learning_log ll_env manage.py
(ll_env) c5220056@GMPTIC:~/myworkplace$ ls learning_log/
__init__.py settings.py urls.py wsgi.py
注: 命令末尾的句點讓新項目使用合適的目錄結(jié)構(gòu),這樣開發(fā)完成后可輕松地將應(yīng)用程序部署到服務(wù)器。
目錄 learning_log 下文件的作用:
- settings.py: 指定 Django 如何與你的系統(tǒng)交互以及如何管理項目。 在開發(fā)項目過程中,我們將修改其中一些設(shè)置,并添加一些設(shè)置。
- urls.py: 告訴 Django應(yīng)創(chuàng)建哪些網(wǎng)頁來響應(yīng)瀏覽器請求。
- wsgi.py: 是 web server gateway interface(Web服務(wù)器網(wǎng)關(guān)接口)的首字母縮寫, 幫助 Django 提供它創(chuàng)建的文件。
創(chuàng)建數(shù)據(jù)庫
(ll_env) c5220056@GMPTIC:~/myworkplace$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
(ll_env) c5220056@GMPTIC:~/myworkplace$ ls
db.sqlite3 learning_log ll_env manage.py
首次執(zhí)行命令 migrate 時,將讓 Django 確保數(shù)據(jù)庫與項目的當(dāng)前狀態(tài)匹配。 在使用 SQLite 的新項目中首次執(zhí)行這個命令時,Django將新建一個數(shù)據(jù)庫。Django 指出它將創(chuàng)建必要的數(shù)據(jù)庫表, 用于存儲我們將在這個項目(Synchronize unmigrated apps, 同步未遷移的應(yīng)用程序)中使用的信息,再確保數(shù)據(jù)庫結(jié)構(gòu)與當(dāng)前代碼(Apply all migrations, 應(yīng)用所有的遷移)匹配。
SQLite是一種使用單個文件的數(shù)據(jù)庫。
查看項目
(ll_env) c5220056@GMPTIC:~/myworkplace$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
January 31, 2018 - 12:46:57
Django version 2.0.1, using settings 'learning_log.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
打開瀏覽器,輸入URL: http://localhost:8000/ 或 http://127.0.0.1:8000

初步建立成功顯示