Install Django
1 | $ pip install django |
Creating a project
1 | $ django-admin startproject mysite |
Start the Project
1 | $ python manage.py runserver |
You’ll see the following output on the command line:
If you want to change the server’s port, pass it as a command-line argument. For instance, this command starts the server on port 8080:
1 | $ python manage.py runserver 8080 |
If you want to change the server’s IP, pass it along with the port. For example, to listen on all available public IPs (which is useful if you are running Vagrant or want to show off your work on other computers on the network), use:
Create a app in a project
1 | $ python manage.py startapp {project name} |
查看 Django 项目的目录结构
切换终端到项目所属目录,使用 tree 命令可以查看项目结构
mac 安装 tree: brew install
ubuntu 安装 tree: sudo apt-get install tree
centos 安装 tree: sudo yum -y install tree
执行 「tree + 项目名」
目录说明
1、djangoDemo/djangoDemo: 项目最初的 Python 包
2、djangoDemo/init.py: 一个空文件,声明所在目录的包为一个 Python 包
3、djangoDemo/settings.py: 管理项目的配置信息
4、djangoDemo/urls.py: 声明请求 url 的映射关系
5、djangoDemo/wsgi.py: python 程序和 web 服务器的通信协议
6、manage.py: 一个命令行工具,用来和 Django 项目进行交互,如前面创建项目就用到了该文件。
项目配置文件 - setting.py
setting.py 文件用来配置整个项目,里面的字段非常多,所以在开始之前有必要先都了解一下默认的配置有哪些
1 | import os |