zoukankan      html  css  js  c++  java
  • 个人站点渲染和跳转过滤功能

    核心逻辑:在url里加入正则,匹配分类、标签、年月日和其后面的参数,在视图函数接收这些参数,然后进行过滤。

    urls.py

    # 个人站点的跳转
    re_path(r'^(?P<username>w+)/(?P<condition>tag|category|archive)/(?P<param>.*)/$', views.home_site),
    
    # 个人站点
    re_path(r'^(?P<username>w+)/$', views.home_site, name='home_site'),

    home_site.py

    def home_site(request, username, **kwargs):
        """
        个人站点视图函数
        :param request:
        :return:
        """
    
        user = UserInfo.objects.filter(username=username).first()
    
        # 判断用户是否存在
        if not user:
            return render(request, 'not_found.html')
    
        article_list = models.Article.objects.filter(user=user)
    
        if kwargs:
            condition = kwargs.get('condition')
            param = kwargs.get('param')
    
            if condition == 'category':
                article_list = article_list.filter(category__title=param)
            elif condition == 'tag':
                article_list = article_list.filter(tags__title=param)
            else:
                year, month = param.split('-')
                article_list = article_list.filter(created_time__year=year, created_time__month=month)
    
        # 查询当前站点
        blog = user.blog
    
        # 获取当前用户或者当前站点对应的所有文章
    
        # 查询当前站点的每一个分类名称以及对应的文章数
        category_list = models.Category.objects.filter(blog=blog).values('pk').annotate(
            count=Count('article__title')).values_list(
            'title', 'count')
    
        # 查询当前站点的每一个标签名称以及对应的文章数
        tag_list = models.Tag.objects.filter(blog=blog).values('pk').annotate(count=Count('article')).values_list(
            'title', 'count'
        )
    
        # 查询当前站点的每一个年月名称以及对应的文章数
        date_list = models.Article.objects.filter(user=user).annotate(month=TruncMonth('created_time')).values_list(
            'month').annotate(
            count=Count('nid')).values_list(
            'month', 'count')
    
        context = {
            'username': username,
            'user': user,
            'blog': blog,
            'article_list': article_list,
            'category_list': category_list,
            'tag_list': tag_list,
            'date_list': date_list,
        }
    
        return render(request, 'home_site.html', context=context)

    home_site.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>{{ request.user.username }}的个人主页</title>
        <link rel="stylesheet" href="/static/blog/css/home_site.css">
        <link rel="stylesheet" href="/static/blog/bs/css/bootstrap.min.css">
    </head>
    <body>
    
    <div class="header">
        <div class="header_content">
            <p class="title">
                <span>{{ blog.title }}</span>
                <a href="" class="backend">管理</a>
            </p>
        </div>
    </div>
    
    <div class="container main_content">
        <div class="row">
            <div class="col-md-3">
                <div class="panel panel-warning">
                    <div class="panel-heading">我的标签</div>
                    <div class="panel-body">
                        {% for tag in tag_list %}
                            <p><a href="/{{ username }}/tag/{{ tag.0 }}">{{ tag.0 }}({{ tag.1 }})</a></p>
                        {% endfor %}
                    </div>
                </div>
    
    
                <div class="panel panel-danger">
                    <div class="panel-heading">随笔分类</div>
                    <div class="panel-body">
                        {% for category in category_list %}
                            <p><a href="/{{ username }}/category/{{ category.0 }}">{{ category.0 }}({{ category.1 }})</a>
                            </p>
                        {% endfor %}
    
                    </div>
                </div>
    
                <div class="panel panel-success">
                    <div class="panel-heading">随笔归档</div>
                    <div class="panel-body">
                        {% for date in date_list %}
                            <p><a href="/{{ username }}/archive/{{ date.0 | date:'Y-m' }} ">{{ date.0 | date:'Y-m' }}
                                ({{ date.1 }})</a></p>
                        {% endfor %}
    
                    </div>
                </div>
    
            </div>
            <div class="col-md-9">
                <div class="article_list">
                    {% for article in article_list %}
                        <div class="article_item clearfix">
                            <h5><a href="">{{ article.title }}</a></h5>
                            <div class="article_desc">
                                {{ article.desc }}
                            </div>
                            <div class="small pub_info pull-right">
                                <span>发布于{{ article.created_time | date:'Y-m-d H:i' }}</span>
                                <span class="glyphicon glyphicon-comment"></span>评论{{ article.comment_count }}
                                <span class="glyphicon glyphicon-thumbs-up"></span>点赞{{ article.up_count }}
                            </div>
                        </div>
                        <hr>
                    {% endfor %}
    
                </div>
    
            </div>
        </div>
    </div>
    </body>
    </html>

  • 相关阅读:
    linux(ubuntu)设置开机启动
    mysql 数据库迁移
    maven GroupId和ArtifactID
    Android内存溢出与内存泄漏
    Android性能优化方向和相关工具
    安卓 px,dpi,dp,ps的区别与联系
    python 深浅拷贝
    公网ip和私网ip
    打开系统设置页
    如何用代码开启“个人热点”功能?如何用代码把iOS设备设置成一个无线路由?
  • 原文地址:https://www.cnblogs.com/lshedward/p/10390716.html
Copyright © 2011-2022 走看看