服务器环境(虚拟机)
- 操作系统版本:CentOS Linux release 7.2.1511 (Core)
- 操作系统内核:3.10.0-327.el7.x86_64
- Python版本:Python 3.5.2
- Django版本:1.10.1
- IP:192.168.127.139
django安装
$ sudo pip3 install django //默认安装最新版本django
如果安装其他版本
$ sudo pip install django==1.8
升级安装
$ sudo pip3 install --upgrade django
- 检查django是否安装成功,方式一
$ python3 -m django --version
1.10.1
- 检查django是否安装成功,方式二
$ python3
Python 3.5.2 (default, Sep 20 2016, 16:45:05)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
1.10.1
django使用
-
创建django链接
$ sudo ln -s /usr/local/python3/bin/django-admin /usr/local/bin/django-admin3
创建项目
$django-admin3 startproject mysite //当前路径下会生成一个mysite目录
$ tree mysite //查看目录结构
mysite
├── manage.py
└── mysite
├── init.py
├── settings.py
├── urls.py
└── wsgi.py
1 directory, 5 files
manage.py 项目入口文件,管理项目启停,
mysite/ 真正的项目目录,存放项目下的文件
init.py 空文件,项目初始化文件,可将所有文件都需要的变量存放进去,即全局变量
settings.py 项目配置文件,如连接数据集等
urls.py 配置项目路由地址信息
wsgi.py
-
启动server(默认端口启动)
$ python3 manage.py runserver //默认端口是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. September 29, 2016 - 03:21:52 Django version 1.10.1, using settings 'mysite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
-
启动server(允许外网访问,修改端口)
$ python3 manage.py runserver 0.0.0.0:8080
//或者 python3 manage.py runserver 8080Performing 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. September 29, 2016 - 03:24:26 Django version 1.10.1, using settings 'mysite.settings' Starting development server at http://0.0.0.0:8080/ Quit the server with CONTROL-C.
-
页面访问:http://192.168.127.139:8080/
创建项目下的应用polls
$ python3 manage.py startapp polls
创建了polls目录,其下目录及文件结构如下:
└── polls
├── admin.py
├── apps.py
├── init.py
├── migrations
│ └── init.py
├── models.py
├── tests.py
└── views.py
polls目录是polls应用的家目录