1.创建新项目
2.数据表设计
2.1.表结构规划
from django.db import models from django.contrib.auth.models import User from django.core.exceptions import ValidationError import datetime # Create your models here. class Article(models.Model): #文章标题 title = models.CharField(max_length=255) #文章简介 brief = models.CharField(null=True,blank=True,max_length=255) #文章板坏 category = models.ForeignKey("Category") #文章内容 content = models.TextField("文章内容") #作者 author = models.ForeignKey("UserProfile") #文章发布时间,auto_now_add自动发布为发布时的时间 pub_date = models.DateTimeField(blank=True,null=True) #最后修改时间为修改的当前时间 last_modify = models.DateTimeField(auto_now=True) #文章置顶,默认是1000 priority = models.IntegerField("优先级",default=1000) #文章状态可选项 status_choice = ((‘draft‘,"草稿"), (‘published‘,"已发布"), (‘hidden‘,‘隐藏‘), ) #文章状态 status = models.CharField(choices=status_choice,default=‘published‘,max_length=64) def __str__(self): return self.title def clean(self): # Don‘t allow draft entries to have a pub_date. if self.status == ‘draft‘ and self.pub_date is not None: raise ValidationError((‘Draft entries may not have a publication date.‘)) # Set the pub_date for published items if it hasn‘t been set already. if self.status == ‘published‘ and self.pub_date is None: self.pub_date = datetime.date.today() class Comment(models.Model): #关联所属文章 article = models.ForeignKey(Article,verbose_name="所属文章") #顶级评论 parent_comment = models.ForeignKey(‘self‘,related_name=‘my_children‘,null=True) #评论及点赞可选项 comment_choice = ((1,‘评论‘), (2,‘点赞‘)) #评论类型 comment_type = models.IntegerField(choices=comment_choice,default=1) #评论用户 user = models.ForeignKey("UserProfile") #评论内容 comment = models.TextField(blank=True,null=True) # 添加评论时间 date = models.DateField(auto_now_add=True) def __str__(self): return "%s,%s,%s" %(self.article,self.comment,self.date) def clean(self): if self.comment_type == 1 and self.comment is None: raise ValidationError("评论写点什么吧") class Category(models.Model): #板块名 name = models.CharField(max_length=64,unique=True) #板块简介 brief = models.CharField(null=True,blank=True,max_length=255) #顶级菜单 set_as_top_menu = models.BooleanField(default=False) #版主设置,可设置为空 admins = models.ManyToManyField("UserProfile",blank=True) #菜单位置 position_index = models.SmallIntegerField() def __str__(self): return self.name class UserProfile(models.Model): #关联到django的的User user = models.OneToOneField(User) #用户名 name = models.CharField(max_length=32) #个人签名 signature = models.CharField(max_length=255,blank=True,null=True) #头像 head_img = models.ImageField(height_field=200,width_field=200) def __str__(self): return self.name
2.2.生成表
3.注册django admin
from django.contrib import admin from bbs import models # Register your models here. class ArticleAdmin(admin.ModelAdmin): list_display = (‘title‘,‘category‘,‘author‘,‘pub_date‘,‘last_modify‘,‘status‘,‘priority‘) class CommentAdmin(admin.ModelAdmin): list_display = (‘article‘,‘parent_comment‘,‘comment_type‘,‘comment‘,‘user‘,‘date‘) class CategoryAdmin(admin.ModelAdmin): list_display = (‘name‘,‘set_as_top_menu‘,‘position_index‘) admin.site.register(models.Article,ArticleAdmin) admin.site.register(models.Comment,CommentAdmin) admin.site.register(models.UserProfile) admin.site.register(models.Category,CategoryAdmin)
4.添加超级用户