django2.0新增功能流程

django2.0新增功能流程

1先在 models.py中,创建字段相关的内容,我这里添加一个博客分类的表 定义数据结构的地方

class PostType(models.Model):
    title = models.CharField(max_length = 60) #分类标题
    order = models.IntegerField() #排序
    timestamp = models.DateTimeField() #创建时间

 

2 执行数据库同步

E:\python\mysite>python manage.py makemigrations blog
Migrations for 'blog':
  blog\migrations\0004_posttype.py
    - Create model PostType

 

E:\python\mysite>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
  Applying blog.0004_posttype... OK

3 通过Admin 管理

通过Admin后台来管理blog表数据。打开 admin.py 文件

from django.contrib import admin
from blog.models import BlogsPost
from blog.models import Member
from blog.models import Link
from blog.models import PostType  #这个是新添加的

# Register your models here.
class BlogsPostAdmin(admin.ModelAdmin):
    list_display = ['title', 'body', 'timestamp']

class MemberAdmin(admin.ModelAdmin):
    list_display = ['username', 'password', 'timestamp']

class LinkAdmin(admin.ModelAdmin):
    list_display = ['title', 'link', 'order', 'timestamp']

class PostTypeAdmin(admin.ModelAdmin):
    list_display = ['title', 'order', 'timestamp']  #这个是新添加的

admin.site.register(BlogsPost, BlogsPostAdmin)
admin.site.register(Member, MemberAdmin)
admin.site.register(Link, LinkAdmin)
admin.site.register(PostType, PostTypeAdmin)  #这个是新添加的
 
      

4 重启服务,打开后台进行访问

django2.0新增功能流程_第1张图片

5 如果想放到线上,那么把文件都上传到服务器,然后执行数据恢复

[root@izwz90bp6do7s3cr45cw6az newblog]# python manage.py  migrate
Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
  Applying blog.0004_posttype... OK

 

6 重启uwsgi,然后登陆后台看下

杀掉端口进程,重新加载

fuser -k 8000/tcp
 uwsgi --ini newblog.ini

 

7 登陆线上后台看看

添加测试,可以成功,有数据

django2.0新增功能流程_第2张图片

 

OK,今天到这里,下一步,看看怎么把分类ID 弄到博客表里面

posted on 2018-06-11 14:33  baker95935 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/baker95935/p/9166919.html

你可能感兴趣的:(django2.0新增功能流程)