Hello World

个人主页

 

 

 

时间分类:

1.用mysql存储

  #date_format

 date_list = models.Article.objects.filter(blog=blog_obj).extra(
        select={"ctime": "date_format(create_time,'%%Y-%%m')"}).values("ctime").annotate(ct=Count("nid"))
   

 

2.用db.sqlite 存储

  #strftime

date_list = models.Article.objects.filter(blog=currentBlog).extra(
        select={"ctime":"strftime('%%Y-%%m-%%d',create_time)"}).values("ctime").annotate(ct=Count('nid'))
   

 

前端展示:

{% for item in date_list %}
       <li><a href="">{{ item.ctime }}({{ item.ct }})</a></li>
{% endfor %}

 

 


分类过滤查询

urls.py

url("^home/(?P<site>\w+)/article/(?P<condition>(category)|(tag)|(date))/(?P<para>\w+-?\w*-?\w*)", views.home),

#考虑到 三类标签(特别是  date)

 

 

views.py

def home(request,*args,**kwargs):

    site = kwargs.get('site')

    #获取用户对象
    user = models.UserInfo.objects.filter(username=site).first()

    # if not user:  #没有此对象,返回 404
    #     return render(request,'NotFound.html')


    # 1.获取当前站点对象
    currentBlog = models.Blog.objects.filter(user=user).first()

    # 2.获取当前站点文章
    article_list =models.Article.objects.filter(blog=currentBlog)
    # print(article_list)


    #3.获取当前站点标签

    tag_list = models.Article.objects.filter(blog=currentBlog).values_list('tags__nid','tags__title').annotate(Count('tags__title'))
    # print('tag_list',tag_list)


    #4.查询当前站点的 Category下每一个分类下的名称以及对应的文章数

    category_list = models.Article.objects.filter(blog=currentBlog).values_list("category__nid", "category__title").annotate(
        cc=Count("nid"))
    # print('category_list',category_list)


    #获取datetime列表

    date_list = models.Article.objects.filter(blog=currentBlog).extra(
        select={"ctime":"strftime('%%Y-%%m-%%d',create_time)"}).values("ctime").annotate(ct=Count('nid'))

    # print(date_list)  #<QuerySet [{'ctime': '2017-09-08', 'ct': 5}, {'ctime': '2017-09-09', 'ct': 1}]>



#----------------------------   分类过滤查询   --------------------------------
    print('!!!!***!!!!!!',kwargs)
    
    

    if kwargs.get("condition") :

        con=kwargs.get("condition")

        if con=="category":

            article_list = models.Article.objects.filter(blog=currentBlog,category_id=kwargs.get("para"))
            # print('article 过滤', article_list)

        elif con=='tag':
            article_list = models.Article.objects.filter(blog=currentBlog, tags__nid=kwargs.get("para"))
            # print('tag 过滤',article_list)


        else:

            # article_list = article_list.extra(
            #     # 使用了extra对日期进行查询。date_format是MySQL的日期处理函数。
            #     where=["strftime( '%%Y-%%m-%%d',create_time)='{0}'".format(kwargs.get("para"))]
            # )

            article_list = models.Article.objects.filter(blog=currentBlog). \
                              extra(where=["strftime('%%Y-%%m-%%d',create_time)='{0}'".format(kwargs.get('para'))])
            print('date 过滤', article_list)





    return render(request,'home.html',{'article_list':article_list,
                                       'user':user,
                                       'tag_list':tag_list,
                                       'category_list':category_list,
                                       'date_list':date_list,
                                       'currentBlog':currentBlog})
View Code

 

.html

<div class="panel panel-info">
                <div class="panel-heading">标签分类</div>
                <div class="panel-body">
                    <ul class="li_s">
                       {% for row in tag_list %}
                            <li><a href="/home/{{ user.username }}/article/tag/{{ row.0 }}">{{ row.1 }}&nbsp;({{ row.2 }})</a></li>
                       {% endfor %}

                    </ul>
                </div>
            </div>


            <div class="panel panel-success">
                <div class="panel-heading">随笔分类</div>
                <div class="panel-body">
                    <ul class="li_s">
                        {% for row in category_list %}
                            <li><a href="/home/{{ user.username }}/article/category/{{ row.0 }}">{{ row.1 }}&nbsp;({{ row.2 }})</a></li>
                        {% endfor %}



                    </ul>
                </div>
            </div>



            <div class="panel panel-info">
                <div class="panel-heading">
                    <h3 class="panel-title">日期分类</h3>
                </div>

                <div class="panel-body">
                    <ul class="li_s">
                        {% for item in date_list %}
                            <li><a href="/home/{{ user.username }}/article/date/{{ item.ctime }}">{{ item.ctime }}({{ item.ct }})</a></li>
                        {% endfor %}



                    </ul>
                </div>
            </div>
        </div>
View Code

 

posted @ 2017-09-04 17:14  nayike  阅读(194)  评论(0编辑  收藏  举报

Hello