1.Web框架
  • 提到Python就一定会听过Python出名的web框架--Django和Flask,Django是比较重量级的框架,Flask就会轻量些
  • Django 是一套用于帮助开发交互式网站的工具。Django能够响应网页请求,还能让你更轻松地读写数据库、管理用户
  • Django 最初被设计用于具有快速开发需求的新闻类站点,目的是要实现简单快捷的网站开发。以下内容简要介绍了如何使用 Django 实现一个数据库驱动的 Web 应用
2.开始Django
  • 首先先安装个Django项目的虚拟环境,虚拟环境是系统的一个位置,你可以在其中安装包,并将其与其他Python包隔离
Learn: python3 -m venv django
  • 激活虚拟环境
➜  Learn source django/bin/activate
(django) ➜  Learn
3.安装
  • 首先当然是有装好Python的前提了
  • 数据库的话Django包含了一个名为 SQLite 的轻量级数据库
    后期开发的时候也可以换成MySQLMongoDB等适合项目开发的数据库
  • 安装Django
# 作者:伊洛Yiluo 
pip3 install django
  • 查看版本号
(django) ➜  Learn python3
Python 3.7.5 (default, Nov 29 2019, 14:32:46)
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
3.1
4.创建Django项目
  • 打开命令行,cd 到一个想放置代码的目录,然后运行以下命令:
(django) ➜  PycharmProjects django-admin startproject AutoPlatform
  • 来查看一下目录的结构
    如果你是Mac 电脑,可以先安装一下tree
brew install tree
➜  ~ tree --help
usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-H baseHREF] [-T title ]
	[-L level [-R]] [-P pattern] [-I pattern] [-o filename] [--version]
	[--help] [--inodes] [--device] [--noreport] [--nolinks] [--dirsfirst]
	[--charset charset] [--filelimit[=]#] [--si] [--timefmt[=]<f>]
	[--sort[=]<name>] [--matchdirs] [--ignore-case] [--fromfile] [--]
	[<directory list>]
  ------- Listing options -------
  -a            All files are listed.
  -d            List directories only.
  -l            Follow symbolic links like directories.
  -f            Print the full path prefix for each file.
  -x            Stay on current filesystem only.
  -L level      Descend only level directories deep.
  • 这回来看一下Django的目录结构
(django) ➜  AutoPlatform tree
.
├── AutoPlatform
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

1 directory, 6 files
  • 最外层的 AutoPlatform/ 根目录是项目的容器, 根目录名称对Django没有影响,可以将它重命名为任何喜欢的名称
  • manage.py: 各种方式管理 Django 项目的命令行工具
    里面一层的 AutoPlatform/ 目录包含你的项目,一个纯 Python 包。它的名字就是当你引用它内部任何东西时需要用到的 Python 包名。 (AutoPlatform.urls)
  • AutoPlatform/__init__.py:一个空文件,告诉 Python 这个目录应该被认为是一个 Python 包
  • AutoPlatform/settings.py:Django 项目的配置文件
  • AutoPlatform/urls.py:Django 项目的 URL 声明,就像网站的“目录”,文件urls.py告诉Django应创建哪些网页来响应浏览器请求
  • AutoPlatform/asgi.py:作为项目的运行在 ASGI 兼容的Web服务器上的入口
  • AutoPlatform/wsgi.py:作为你的项目的运行在 WSGI 兼容的Web服务器上的入口,文件名是web server gateway interface(Web服务器网关接口)的首字母缩写
5.启动项目
  • 来确认一下Django 项目是否真的创建成功了
(django) ➜  AutoPlatform python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 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.
August 24, 2020 - 10:14:06
Django version 3.1, using settings 'AutoPlatform.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
  • 忽略有关未应用最新数据库迁移的警告,刚刚启动的是 Django 自带的用于开发的简易服务器,它是一个用纯 Python 写的轻量级的 Web 服务器

  • 现在,服务器正在运行,浏览器访问 https://127.0.0.1:8000/
  • 小火箭正在飞,一切正常,这就启动了Django

NOTE: 用于开发的服务器在需要的情况下会对每一次的访问请求重新载入一遍 Python 代码。所以不需要为了让修改的代码生效而频繁的重新启动服务器。然而,一些动作,比如添加新文件,将不会触发自动重新加载,这时得自己手动重启服务器