Django入门

创建项目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

 

 连接测试:

视图和URL 设置

 

posted @ 2016-05-27 10:06  侠之大者kamil  阅读(7308)  评论(0编辑  收藏  举报