创建新的项目
django-admin.py startproject my_project
创建新的App
# 在Django项目(my_project)的根目录下执行
python manage.py startapp my_app
数据库迁移
# 创建迁移
python manage.py makemigrations
# 应用迁移
python manage.py migrate
启动Django服务
python manage.py runserver
创建models
class App1(models.Model):
title = models.CharField(max_length=100)
category = models.CharField(max_length=50, blank=True)
date_time = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.title
class Meta:
ordering = ['-date_time']
进入Django中的交互式shell
python3 manage.py shell
from app1.models import App1
# create(增加操作)
App1.objects.create(title='title',category='category1')
# all和get(查看操作)
App1.objects.all()
App1.objects.get(id=1)
# update(修改操作)
demo = App1.objects.get(id=1)
demo.title
demo.title='title2'
# delete(删除操作)
demo.delete()
创建一个超级用户
python3 manage.py createsuperuser
# 输入http://127.0.0.1:8000/admin登录后台
评论 (0)