virtualenv/virtualenvwrapper设置见另一篇
- 初始化工程
django-admin startproject mysite
如果用pycharm,可以直接在里面建立django工程(可以选择virtualenv目录),生成的目录和此处一样
- 在项目settings.py里设定了数据库类型后,运行
python manage.py migrate
初始化数据库
- 新建app
python manage.py startapp newapp
- 编写model类定义
#coding: utf-8
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class User(models.Model):
name = models.CharField(max_length=10)
pwd = models.CharField(max_length=50)
role = models.IntegerField()
regis_time = models.DateTimeField('注册时间')
login_time = models.DateTimeField('最后登录')
class Blogs(models.Model):
title = models.CharField(max_length=100)
content = models.CharField()
post_by = models.ForeignKey(User)
- 在站点setting.py文件里加入此新加的app:
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'newapp',
]
生成迁移脚本,新建立项目时需要运行这一步(无新加项目就无需运行)
$ python manage.py makemigrations polls
数据库变动提交
$ python manage.py migrate