1 Environment Construction
sudo apt-get install python3-pip 安装pip3 sudo pip3 install virtualenv 安装虚拟环境,这里展示virtualenv virtualenv -p python3 DjangoEnv 创建django虚拟环境DjangoEnv(名称根据项目名确定) source DjangoEnv/bin/activate 进入对应的虚拟环境 pip freeze pip install django==1.11 安装django版本为1.11
Optional Redis Installation:
sudo apt-get update 更新软件源 sudo apt-get install redis-server 安装redis redis-server 启动redis(前台启动,后台需要改配置文件) redis-cli 查看redis是否启动
2 creation of projects and apps
Create a Django project in a virtual environment
django-admin startproject 项目名
Create App App
python manage.py startapp APP名
Build a table in models.py
举例说明:class Wheel(models.Model): img = models.CharField(max_length=150) name = models.CharField(max_length=20) trackid = models.CharField(max_length=20)
3 database creation and settings configuration
Create a database
create database 数据库名
To set administrative permissions for a database (optional)
进入mysql中 grant all on *.* to ‘django‘@‘%‘ identified by ‘123456‘ flush privileges 用户名:django 密码:123456
settings.py Configuration
app配置 INSTALLED_APPS = [添加app名]数据库配置 DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, #修改为mysql ‘NAME‘: ‘项目名‘, ‘USER‘:‘数据库用户名‘, ‘PASSWORD‘:‘密码‘, ‘PORT‘:3306 #端口 } }
4 Migrating files
python manage.py makemigrations APP名 生成迁移文件,产生一个migrations的文件里面0001.initial.pypython manage.py sqlmigrate 0001.initial 查看迁移文件python manage.py migrate App名 执行迁移文件
The resulting data table can be viewed in the database.
5 Start Test
python manage.py runserver 启动服务器,这是django自带的轻量级服务器方便测试python manage.py runserver 127.0.0.1:8080 指定本机访问注意 : runserver开启之后进行下一项目记得及时关闭,不然会被占用python manage.py runserver 0.0.0.0:8000 不指定本机访问
Django Project Setup (Ubuntu system)