Django 學(xué)習(xí)日記 - django-admin.py&manage.py - step1

1 知識點(diǎn)

  1. django-admin.py是Django的一個(gè)用于管理任務(wù)的命令行工具,
  2. manage.py是對django-admin.py的簡單包裝,每個(gè)Django Project里面都會包含一個(gè)manage.py,要創(chuàng)建項(xiàng)目才會有

2 語法

django-admin.py <subcommand> [options]
manage.py <subcommand>  [options]

subcommand是子命令;options是可選的

常用子命令:
startproject: 創(chuàng)建一個(gè)項(xiàng)目 (*)
startapp:創(chuàng)建一個(gè)app (*)
runserver:運(yùn)行開發(fā)服務(wù)器 (*)
shell:進(jìn)入django shell (*)
dbshell:進(jìn)入django dbshell
check:檢查django項(xiàng)目完整性
flush:清空數(shù)據(jù)庫
compilemessages:編譯語言文件(*)
makemessages:創(chuàng)建語言文件(*)
makemigrations:生成數(shù)據(jù)庫同步腳本(第一次使用) (*)
migrate:同步數(shù)據(jù)庫 (*)
showmigrations:查看生成的數(shù)據(jù)庫同步腳本 (*)
sqlflush:查看生成清空數(shù)據(jù)庫的腳本 (*)
sqlmigrate:查看數(shù)據(jù)庫同步的sql語句 (*)
dumpdata:導(dǎo)出數(shù)據(jù)
loaddata:導(dǎo)入數(shù)據(jù)
diffsettings: 查看你的配置和django默認(rèn)配置的不同之處

3 獲取幫助文檔

(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\class04\hello_django>django-admin.py help startproject
usage: django-admin.py startproject [-h] [--version] [-v {0,1,2,3}]
                                    [--settings SETTINGS]
                                    [--pythonpath PYTHONPATH] [--traceback]
                                    [--no-color] [--template TEMPLATE]
                                    [--extension EXTENSIONS] [--name FILES]
                                    name [directory]

Creates a Django project directory structure for the given project name in the
current directory or optionally in the given directory.

positional arguments:
  name                  Name of the application or project.
  directory             Optional destination directory

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Raise on CommandError exceptions
  --no-color            Don't colorize the command output.
  --template TEMPLATE   The path or URL to load the template from.
  --extension EXTENSIONS, -e EXTENSIONS
                        The file extension(s) to render (default: "py").
                        Separate multiple extensions with commas, or use -e
                        multiple times.
  --name FILES, -n FILES
                        The file name(s) to render. Separate multiple
                        extensions with commas, or use -n multiple times.

4 manage.py特有的一些子命令

createsuperuser: 創(chuàng)建超級管理員 (*)
changepassword: 修改密碼 (*)
clearsessions: 清除session

5 實(shí)驗(yàn)步驟

5.1 創(chuàng)建project和創(chuàng)建app

(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\learn04>django-admin.py startproject hello_django
(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\learn04>cd hello_django
(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\learn04\hello_django>django-admin.py startapp hello

5.2 啟動項(xiàng)目并測試

(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\learn04\hello_django>manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
January 09, 2017 - 22:21:15
Django version 1.9.7, using settings 'hello_django.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
訪問測試

5.2 同步數(shù)據(jù)庫

(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\learn04\hello_django>manage.py makemigrations
No changes detected

(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\learn04\hello_django>manage.py migrate
Operations to perform:
  Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
  Rendering model states... DONE
  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 sessions.0001_initial... OK

5.3 創(chuàng)建管理員賬戶登錄管理后臺

(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\class04\hello_django>manage.py createsuperuser
Username (leave blank to use 'spareribs'): admin
Email address: admin@admin.com
Password:
Password (again):
Superuser created successfully.
登陸admin管理后臺
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容