Django專案-bbs論壇(1)
阿新 • • 發佈:2019-01-02
專案:開發一個簡單的BBS論壇
需求:
- 整體參考“抽屜新熱榜” + “虎嗅網”
- 實現不同論壇版塊
- 帖子列表展示
- 帖子評論數、點贊數展示
- 線上使用者展示
- 允許登入使用者發貼、評論、點贊
- 允許上傳檔案
- 帖子可被置頂
- 可進行多級評論
知識必備:
- Django
- HTML\CSS\JS
- BootStrap
- Jquery
from django.db import models from django.contrib.auth.models import User # Create your models here. class UserProfile(models.Model): """使用者"""使用django自帶的admin管理資料庫->admin: 首先建立superuser使用者用於登入admin自帶的web: python manage.py createsuperuseruser = models.OneToOneField(User,null=True,default=None) name = models.CharField(max_length=32) def __str__(self): return self.name class Article(models.Model): """文章""" title = models.CharField(max_length=128,unique=True) author = models.ForeignKey("UserProfile") category = models.ForeignKey("Category") pub_date = models.DateTimeField(auto_now_add=True,auto_created=True) tags = models.ManyToManyField("Tag",null=True) body = models.TextField(max_length=100000) head_img = models.ImageField(upload_to="uploads") status_choices = ((0,"草稿"),(1,"釋出"),(2,"隱藏")) priority = models.SmallIntegerField(default=1000,verbose_name="優先順序") def __str__(self): return self.title class Category(models.Model): """板塊""" name = models.CharField(max_length=64,unique=True) set_as_to_menu = models.BooleanField(default=True) def __str__(self): return self.name class Tag(models.Model): """標籤""" name = models.CharField(max_length=64,unique=True) def __str__(self): return self.name class Comment(models.Model): """評論""" article = models.ForeignKey("Article") p_node = models.ForeignKey("Comment",null=True,blank=True,related_name="my_child_comment") user = models.ForeignKey("UserProfile") date = models.DateTimeField(auto_now_add=True) comment = models.TextField(max_length=1024) def __str__(self): return self.comment class Like(models.Model): """點贊""" article = models.ForeignKey("Article") user = models.ForeignKey("UserProfile") date = models.DateTimeField(auto_now_add=True) class PrivateMail(models.Model): """私信""" pass
其次admin模組程式碼: 注意:需要使用admin管理的表都需要註冊一下
from django.contrib import admin # Register your models here. from bbs import models admin.site.register(models.UserProfile) admin.site.register(models.Article) admin.site.register(models.Comment) admin.site.register(models.Category) admin.site.register(models.Tag) admin.site.register(models.Like) admin.site.register(models.PrivateMail)
新增使用者並激活授權則使用者能登入並有相應的許可權。
介面: