安装
Django是以Python为语言环境的,所以要先确保计算机上已经安装了Python。
Linux
ubuntu:
sudo pip install Django==1.11.7
安装中指定了版本,可以修改版本号,或者不指定将安装软件源中已有的版本。
安装完成后,可以进入到Python交互模式中查看版本:
>>> import django >>> print(django.get_version()) 1.11.7
上面是简单而常用的方法,除了这种方法,还可以下载Django的源码进行安装:
git clone https://github.com/django/django.git
此时会在当前目录中看到一个名为django的目录,里面是最新版本的Django,随后在当前目录下执行如下操作:
sudo pip install -e ./django
系统会提示Django已经安装成功的信息:"Successfully installed Django"。
Windows
在cmd中执行:
pip install django==1.11.7
创建项目
方法一:
λ django-admin startproject mysite
λ tree /f
D:.
└─mysite
│ manage.py
│
└─mysite
settings.py
urls.py
wsgi.py
__init__.py
方法二,在项目名称mysite后面有一个空格,然后还有一个句号(英文半角句号),如此也可以创建项目,只不过是在当前文件夹下创建。
λ django-admin startproject mysite .
λ tree /f
D:.
│ manage.py
│
└─mysite
settings.py
urls.py
wsgi.py
__init__.py
启动服务
进入与manage.py同目录下执行:
python manage.py runserver
会看到如下信息:
Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
此时再浏览器中输入http://127.0.0.1:8000/ 或者http://localhost:8000/,就会看到如下图所示的结果。

创建应用
项目已经创建好了,网站也有了,接下来要实现网站的具体功能,在Django中,人们把这些具体的功能称之为‘‘应用‘‘(application)。
λ python manage.py startapp blog
λ tree /f
D:.
│ db.sqlite3
│ manage.py
│
├─blog
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ views.py
│ │ __init__.py
│ │
│ └─migrations
│ __init__.py
│
└─mysite
│ settings.py
│ urls.py
│ wsgi.py
│ __init__.py
│
└─__pycache__
settings.cpython-36.pyc
urls.cpython-36.pyc
wsgi.cpython-36.pyc
__init__.cpython-36.pyc
下面对生成的文件进行简要说明
Django的任务管理命令行工具 django-admin.py
λ django-admin
Type ‘django-admin 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
查看帮助信息:
D:\lcg\mysite
λ django-admin help startapp
usage: django-admin startapp [-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 app directory structure for the given app 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.
manage.py
D:\lcg\mysite
λ python manage.py
Type ‘manage.py help <subcommand>‘ for help on a specific subcommand.
Available subcommands:
[auth]
changepassword
createsuperuser
[contenttypes]
remove_stale_contenttypes
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
与django-admin进行对比,发现代码有一部分相同,同时manage.py还有自己的特点。
django-admin命令对应着django-admin.py文件,她在Django安装后保存在Django安装目录下。(windows的C:\Python36\Scripts,linux的/bin)
而manage.py只是建立了一个项目之后,才保存于项目的根目录之中。
可以说manage.py是对django-admin的简单封装。
关于两者的更多辨析,请参阅官方文档:https://docs.djangoproject.com/en/1.11/ref/django-admin/
mysite
mysite是所建项目的管理功能目录,这个目录的名称因用户所创建的项目名称的不同而异,它里面的几个文件常用于向整个项目进行参数配置。
- settings.py:这个文件中包含了项目的初始化设置,可以针对整个项目进行有关参数的配置,比如配置数据库、添加应用等。
- urls.py:这是一个URL配置表文件,主要将URL映射到应用程序上,当用户请求某个URL时,Django项目会根据这个文件中的映射关系指向某个目标对象,该对象可以是某个应用的urls.py也可以是某个具体的视图函数。在Django中,这个文件也被称之为URLconf,这是Django非常强大的一个特性。
- wsgi.py:WSGI是Web Server Gateway Interface的缩写。WSGI是Python所选择的服务器和应用标准,Django也会使用。wsgi.py文件定义了我们所创建的项目都是WSGI应用。有关于WSGI的更多知识请参阅官方文档:https://wsgi.readthedocs.io/en/latest/index.html
- __pycache__:启动服务,浏览项目的时候产生的。不浏览就不会产生,只有网站运行后它才会出现,它其就是编译后的一个文件夹。里面都是以.pyc结尾的文件。
blog
blog是项目中所创建的项目之一,用创建项目的指令还可以创建很多其他的应用,每创建一个应用,Django都会在项目的根目录中创建一个子目录,并且目录中会有以下默认的文件: