个人博客开发-04(基于Python-3.8 + Django-2.2):视图函数

7.视图函数

初学,写写笔记,权当交流,代码也写的不好~
本来是可以用模板继承来减少代码量的,但是前端继承那里继承来继承去显示样式都调试不合适~容我再去看看书了再来

from django.shortcuts import render, redirect
from .models import Blog, Category, Tag, Comment
import markdown
from django.contrib import messages


# 首页
def index_handler(request):
    context = {}
    context['paihang_blog'] = Blog.objects.all().order_by('-viewsCount')[:2] # 点击排行
    context['blog_list'] = Blog.objects.all().order_by('-createDatetime') # 按照时间排序的博客
    context['tag_list'] = Tag.objects.all()	# 所有标签
    return render(request, 'index.html', context=context)


# 关于
def about_handler(request):
    # 渲染到关于我页面
    return render(request, 'about.html')


# 博客详细页
def detail_handler(request, pk):
    # 博客详细页需要传入博客的id
    context = {}
    # 获取到最后一篇博客
    last_blog = Blog.objects.all().order_by('-createDatetime').reverse()[0]
    # 获取到第一篇博客
    first_blog = Blog.objects.all().order_by('-createDatetime')[0]
    # 获取到当前浏览的博客
    blog = Blog.objects.get(id=pk)
    # 当调用博客详细页函数一次,则浏览量增加一次
    blog.increase_views()
    '''
        此处判断逻辑关系:
        如果当前博客是最后一篇或第一篇,怎返回信息表明是最后一篇或第一篇,在前端代码中判断
        如果是中间博客,遍历所有博客,获取当前博客的前一篇博客和后一篇博客
        注:此处有一处bug,如果博客数量小于2篇,阅读原文会报异常
    '''
    if last_blog == first_blog:
        context['pre_message'] = '已经是第一篇'
        context['next_message'] = '已经是最后一篇'
    if blog == first_blog:
        context['pre_message'] = '已经是第一篇'
        context['next_blog'] = Blog.objects.all().order_by('-createDatetime')[1]
    if blog == last_blog:
        context['next_message'] = '已经是最后一篇'
        context['pre_blog'] = Blog.objects.all().order_by('-createDatetime').reverse()[1]
    i = 0
    if Blog.objects.all().count() >= 3 and blog != last_blog and blog != first_blog:
        for article in Blog.objects.all().order_by('-createDatetime'):
            i = i + 1
            if article == blog:
                break
        context['pre_blog'] = Blog.objects.all().order_by('-createDatetime')[i-2]
        context['next_blog'] = Blog.objects.all().order_by('-createDatetime')[i]
    # 获取到博客之后,将其内容支持 markown语法和代码高亮
    blog.text = markdown.markdown(blog.text,
        extensions=[
            'markdown.extensions.extra',
            'markdown.extensions.codehilite',
        ]
    )
    context = {
        'blog':blog,
        'blog_list':Category.objects.get(name=blog.category).category_set.all().order_by('-createDatetime'),
        'tag_list':Tag.objects.all(),
        'category_list':Category.objects.all(),
        'paihang_blog':Blog.objects.all().order_by('-viewsCount')[:2]
    }
    return render(request, 'detail.html', context=context)

# 学无止境(博客列表)
def study_handler(request):
    context = {
        'blog_list':Blog.objects.all().order_by('-createDatetime'),
        'tag_list':Tag.objects.all(),
        'category_list':Category.objects.all(),
        'paihang_blog':Blog.objects.all().order_by('-viewsCount')[:2],
        'message':'学无止境'
    }
    return render(request, 'study.html', context=context)


# 留言
def message_handler(request):
    context = {
        'comment_list':Comment.objects.all().order_by('-created_time'),
        'comment_sum':Comment.objects.all().count()
    }
    return render(request, 'message.html', context=context)


# 该标签下的博客
def tag_handler(request, pk):
    tag = Tag.objects.get(id=pk)
    context = {
        'blog_list':tag.tags_set.all().order_by('-createDatetime'),
        'tag_list':Tag.objects.all(),
        'category_list':Category.objects.all(),
        'paihang_blog':Blog.objects.all().order_by('-viewsCount')[:2],
        'message':tag.name
    }
    return render(request, 'study.html', context=context)


# 该分类下的博客
def category_handler(request, pk):
    category = Category.objects.get(id=pk)
    context = {
        'blog_list':category.category_set.all().order_by('-createDatetime'),
        'tag_list':Tag.objects.all(),
        'category_list':Category.objects.all(),
        'paihang_blog':Blog.objects.all().order_by('-viewsCount')[:2],
        'message':category.name
    }
    return render(request, 'study.html', context=context)

# 搜索
def search_handler(request):
    # 获取搜索的关键词
    submit = request.GET.get('keyboard')
    if not submit:
        error_msg = "请输入搜索关键词"
        messages.add_message(request, messages.ERROR, error_msg, extra_tags='danger')
        # 若没有关键词,重定向到主页
        return redirect('index')
    
    context = {
        'blog_list':Blog.objects.filter(blogTitle__icontains=submit).order_by('-createDatetime'),
        'tag_list':Tag.objects.all(),
        'category_list':Category.objects.all(),
        'paihang_blog':Blog.objects.all().order_by('-viewsCount')[:2],
        'message':'学无止境'
    }
    return render(request, 'study.html', context=context)


# 留言提交
def form_handler(request):
    if request.method == 'GET':
        nickname = request.GET.get('nickname')
        email = request.GET.get('email')
        text = request.GET.get('lytext')
        c = Comment()
        c.nickname = nickname
        c.text = text
        c.email = email
        c.save()
        messages.add_message(request, messages.SUCCESS, '留言发表成功!', extra_tags='success')
    return redirect('message')


def slowlife_handler(request):
    return redirect('index')

参考博客:

  1. 使用QuerySet查询时,可以参考:Django QuerySet 进阶
  2. 开发个人博客比较完整的一篇博客 django开发个人博客系统

你可能感兴趣的:(Python)