zoukankan      html  css  js  c++  java
  • Django博客项目

    1、项目需求

      1、form和Ajax实现用户的注册和登录功能

      2、博客首页的部分用户和文章的列表渲染

      3、个人主页的页面设计

      4、文章详情页面展示

      5、文章的评论功能

      6、点赞与取消功能

      7、用户后台管理页面

      8、富文本编辑器的使用

      9、防止XSS攻击

                                               

    2、设计表结构

                      

    model代码:

    class BlogUser(AbstractUser):
        """用户表"""
        username = models.CharField(verbose_name='用户名', max_length=20, unique=True)
        password = models.CharField(verbose_name='密码', max_length=256)
        avatar = models.FileField(verbose_name='用户头像', upload_to='avatar', default='avatar/head.jpg')
        email = models.EmailField(verbose_name='注册邮箱', unique=True)
        create_time = models.DateTimeField(verbose_name='注册时间', auto_now_add=True)
        blog = models.OneToOneField('Blog', on_delete=models.CASCADE)
        has_confirmed = models.BooleanField(default=False)
    
        def __str__(self):
            return self.username
    
        class Meta:
            verbose_name = '用户信息'
            verbose_name_plural = verbose_name
    
    
    class Confirm(models.Model):
        """邮箱确认表"""
        code = models.CharField(verbose_name='确认码', max_length=256)
        user = models.OneToOneField('BlogUser', on_delete=models.CASCADE)
        create_time = models.DateTimeField(auto_now_add=True)
    
        def __str__(self):
            return self.user.username + ': ' + self.code
    
        class Meta:
            ordering = ['-create_time']
            verbose_name = '确认码'
            verbose_name_plural = verbose_name
    
    
    class Blog(models.Model):
        """博客表"""
        title = models.CharField(verbose_name='博客标题', max_length=64)
    
        def __str__(self):
            return self.title
    
        class Meta:
            verbose_name = '博客信息'
            verbose_name_plural = verbose_name
    
    
    class Category(models.Model):
        """文章类别表"""
        title = models.CharField(verbose_name='分类标题', max_length=32)
        blog = models.ForeignKey('Blog', verbose_name='所属博客', on_delete=models.CASCADE)
    
        def __str__(self):
            return self.title
    
        class Meta:
            verbose_name = '文章类别'
            verbose_name_plural = '文章类别'
    
    
    class Tag(models.Model):
        """文章标签表"""
        title = models.CharField(verbose_name='标签标题', max_length=32)
        blog = models.ForeignKey('Blog', verbose_name='所属博客', on_delete=models.CASCADE)
    
        def __str__(self):
            return self.title
    
        class Meta:
            verbose_name = '文章标签'
            verbose_name_plural = '文章标签'
    
    
    class Article(models.Model):
        """文章表"""
        user = models.ForeignKey('BlogUser', verbose_name='用户', on_delete=models.CASCADE)
        title = models.CharField(verbose_name='标题', max_length=100)
        desc = models.CharField(verbose_name='文章描述', max_length=250)
        content = models.TextField(verbose_name='正文')
        create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
        modify_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    
        click_nums = models.IntegerField(verbose_name='点击量', default=0)
        comment_nums = models.IntegerField(verbose_name='评论量', default=0)
        praise_nums = models.IntegerField(verbose_name='点赞量', default=0)
    
        category = models.ForeignKey('Category', verbose_name='文章类别', default='', on_delete=models.CASCADE,)
        tag = models.ManyToManyField('Tag', verbose_name='文章标签', default='')
    
        def __str__(self):
            return self.title
    
        class Meta:
            verbose_name = '博客文章'
            verbose_name_plural = verbose_name
    
    
    class Comment(models.Model):
        """评论表"""
        article = models.ForeignKey('Article', verbose_name='评论文章', on_delete=models.CASCADE)
        user = models.ForeignKey('BlogUser', verbose_name='评论者', on_delete=models.CASCADE)
        pub_date = models.DateTimeField(verbose_name='评论发布时间', default=timezone.now)   # 测试版本,上线时添加 auto_now_add=True
        content = models.TextField(verbose_name='评论内容')
        parent_comment = models.ForeignKey('self', verbose_name='回复', null=True, on_delete=models.CASCADE)
    
        def __str__(self):
            return self.content
    
        class Meta:
            verbose_name = '评论'
            verbose_name_plural = verbose_name
    
    
    class Praise(models.Model):
        """点赞表"""
        article = models.ForeignKey('Article', verbose_name='点赞文章', on_delete=models.CASCADE)
        user = models.ForeignKey('BlogUser', verbose_name='点赞者', on_delete=models.CASCADE)
        is_praise = models.BooleanField(default=False)
    
        class Meta:
            unique_together = [
                ('article', 'is_praise'),
            ]
            verbose_name = '点赞'
            verbose_name_plural = verbose_name
    View Code

    3、功能实现

    4、功能测试

    5、......

    终日不为以思,无益,不如学也
  • 相关阅读:
    轻松掌握Ajax.net系列教程十二:使用TabContainer&TabPanel
    轻松掌握Ajax.net系列教程十五:使用AutoCompleteExtender
    一步一步学Linq to sql(二):DataContext与实体
    一步一步学Linq to sql(十):分层构架的例子
    轻松掌握Ajax.net系列教程十四:使用CalendarExtender
    一步一步学Linq to sql(八):继承与关系
    整理了一些tsql技巧
    一步一步学Linq to sql(三):增删改
    数据库连接字符串大全
    海量数据库的查询优化及分页算法方案
  • 原文地址:https://www.cnblogs.com/lymlike/p/11542111.html
Copyright © 2011-2022 走看看