創建項目HelloWord
命令簡介
kamil@ubuntu:~/opt$ django-admin.py Type 'django-admin.py help <subcommand>' for help on a specific subcommand. Available subcommands: [django] check compilemessages createcachetable dbshell diffsettings dumpdata flush inspectdb loaddata makemessages makemigrations migrate runserver sendtestemail shell showmigrations sqlflush sqlmigrate sqlsequencereset squashmigrations startapp startproject test testserver Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.). kamil@ubuntu:~/opt$
創建項目:
kamil@ubuntu:~/opt$ django-admin.py startproject HelloWord kamil@ubuntu:~/opt$ ls HelloWord kamil@ubuntu:~/opt$ sudo apt-get install tree Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: tree 0 upgraded, 1 newly installed, 0 to remove and 12 not upgraded. Need to get 40.6 kB of archives. After this operation, 138 kB of additional disk space will be used. Get:1 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 tree amd64 1.7.0-3 [40.6 kB] Fetched 40.6 kB in 1s (39.2 kB/s) Selecting previously unselected package tree. (Reading database ... 207293 files and directories currently installed.) Preparing to unpack .../tree_1.7.0-3_amd64.deb ... Unpacking tree (1.7.0-3) ... Processing triggers for man-db (2.7.5-1) ... Setting up tree (1.7.0-3) ... kamil@ubuntu:~/opt$ tree . └── HelloWord ├── HelloWord │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── manage.py 2 directories, 5 files kamil@ubuntu:~/opt$ cd HelloWord/ kamil@ubuntu:~/opt/HelloWord$ ls HelloWord manage.py kamil@ubuntu:~/opt/HelloWord$
文件介紹
1 在 helloword/ 根目錄上級僅僅是一個容器項目。對於Django它的名字隨意,可以將其重命名為任何你喜歡的。 2 manage.py: 一個命令行工具,可以讓您以各種方式與Django項目進行交互。你可以閱讀所有關於manage.py 在 django-admin和manage.py 的細節。 3 內部 helloword/目錄是實際項目的Python包。它的名字是你需要使用導入里面的任何Python包的名稱(例如helloword.urls)。 4 helloword/__init__.py: 一個空文件,該文件告訴Python這個目錄應該作為一個Python包。 5 helloword/settings.py: 設置/配置這個Django項目。 Django的設置,會告訴你如何設置工作。 6 helloword/urls.py: 該 URL 聲明這個Django項目; 類似Django網站的一個“表的內容”。 7 helloword/wsgi.py: 一個WSGI兼容Web服務器的入口點,以滿足您的項目需要。
啟動項目並測試
kamil@ubuntu:~/opt/HelloWord$ python3 manage.py runserver 0.0.0.0:8000 Performing system checks... System check identified no issues (0 silenced). You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. May 27, 2016 - 01:15:40 Django version 1.10a1, using settings 'HelloWord.settings' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C. [27/May/2016 01:16:29] "GET / HTTP/1.1" 200 1767 [27/May/2016 01:16:43] "GET / HTTP/1.1" 200 1767 Not Found: /favicon.ico [27/May/2016 01:16:43] "GET /favicon.ico HTTP/1.1" 404 1940