I. Database table design (models.py file)
#!/usr/bin/env python
#-*-Coding:utf-8-*-
From __future__ import unicode_literals
From django.db import Models
#导入django用户认证表
From Django.contrib.auth.models import User
# Create your models here.
Class article (models. Model): #帖子表
title = models. Charfield (U "article title", Max_length=255,unique=true)
Categroy = models. ForeignKey ("Category", Verbose_name=u "plate")
Head_img = models. ImageField (upload_to= "uploads") #每个帖子都要有一张图片
Content = models. TextField (U "article content") #帖子内容
Author = models. ForeignKey ("UserProfile") #一篇文章只能有一个作者
Publish_date = models. Datetimefield (auto_now=true) #帖子发布日期
Hidden =models. Bigautofield (default=true) #帖子是否显示
Priority = Models. Integerfield (U "priority", default=1000) #帖子优先级
def __unicode__ (self): #设置默认返回值
Return "<%s,author:%s>"% (Self.title,self.author)
Class Comment (models. Model): #评论表
Article = models. ForeignKey ("article") #一条评论只能针对一篇文章
user = models. ForeignKey ("UserProfile") #由哪个用户发出评论
#parent_comment = models. ForeignKey (' Comment ',) #父级评论
Parent_comment = models. ForeignKey ("self", related_name = ' parcomment ', blank=true,null=true) #父级评论
Date = models. Datetimefield (auto_now=true) #评论发布日期
Comment = models. TextField (max_length=1000) #评论内容
def __unicode__ (self): Returns the default value
Return "<%s,user:%s>"% (Self.comment,self.user)
Class Thumbup (models. Model): #点赞统计
Article = models. ForeignKey (' article ') #点赞的文章
user = models. ForeignKey (' userprofile ') #点赞的用户
Date = models. Datetimefield (auto_now=true) #人点赞日期
def __unicode__ (self): #设置默认返回值
Return "<user:%s>"% (Self.user)
Class Category (models. Model): #板块表
Name = models. Charfield (max_length=64,unique=true) #板块名字
admin = models. Manytomanyfield ("UserProfile") #板块管理员多对多, many-to-many is a two-way
def __unicode__ (self):
Return Self.name
Class UserProfile (models. Model): #用户信息表
user = models. Onetoonefield (User) #继承系统原生的User表
Name = models. Charfield (MAX_LENGTH=32)
Groups = models. Manytomanyfield ("UserGroup") #属于哪个用户组
def __unicode__ (self):
Return Self.name
Class UserGroup (models. Model): #用户组表
Name = models. Charfield (max_length=64,unique=true) #用户组名称
def __unicode__ (self):
Return Self.name
Second, the initialization of data:
Database access settings in settings.py
DATABASES = {
' Default ': {
' ENGINE ': ' Django.db.backends.mysql ',
' NAME ': ' S11bbs ',
' HOST ': ' 127.0.0.1 ',
' USER ': ' Root ',
' PASSWORD ': ' 123456 ',
}
}
Before you perform the synchronization, create the database S11bbs before executing the following statement
Python manage.py Migrate
mysql> use S11bbs;
Database changed
Mysql> Show tables;
+----------------------------+
| Tables_in_s11bbs |
+----------------------------+
| Auth_group |
| auth_group_permissions |
| auth_permission |
| Auth_User |
| auth_user_groups |
| auth_user_user_permissions |
| Django_admin_log |
| Django_content_type |
| django_migrations |
| django_session |
+----------------------------+
Rows in Set (0.00 sec)
Python manage.py Migrations
Python manage.py Migrate
Mysql> Show tables;
+----------------------------+
| Tables_in_s11bbs |
+----------------------------+
| Auth_group |
| auth_group_permissions |
| auth_permission |
| Auth_User |
| auth_user_groups |
| auth_user_user_permissions |
| Django_admin_log |
| Django_content_type |
| django_migrations |
| django_session |
| web_article |
| Web_category |
| Web_category_admin |
| web_comment |
| Web_thumbup |
| Web_usergroup |
| Web_userprofile |
| web_userprofile_groups |
+----------------------------+
Rows in Set (0.00 sec)
Mysql>
Third, register to the admin to establish the database table
Register the created table in admin
From Django.contrib Import admin
Import Models
# Register your models here.
Admin.site.register (models. article)
Admin.site.register (models. Category)
Admin.site.register (models.comment)
Admin.site.register (models. THUMBUP)
Admin.site.register (models. UserProfile)
Admin.site.register (models. UserGroup)
Admin.site.register (models. article)
Create User Ucode
C:\users\ryan\pycharmprojects\s11bbs>python manage.py Createsuperuser
Username (leave blank to use ' Ryan '): Ucode
Email Address:
Password:
Password (again):
Superuser created successfully.
C:\users\ryan\pycharmprojects\s11bbs>
This article from "Flat Light is true" blog, please be sure to keep this source http://ucode.blog.51cto.com/10837891/1868020
Develop a BBS case based on Django