跳轉個人主頁的指定標籤內容

weixin_34402408發表於2018-03-15
6692028-4cd43dfaafe419cb.png
image.png

一、思路

url(r'^(?P<site>\w+)/(?P<condition>((tag)|(data)|(category)))/(?P<val>\w+-*\w*).html$', home.filter),
=
跳轉到不同的標籤目錄的時候,我們在url做手腳,讓url整體選擇跳轉,進行判斷應該進入那個分支去提取資訊

def filter(request,**kwargs):
    site = kwargs['site']
    condition = kwargs['condition']
    val = kwargs['val']
    blog = models.Blog.objects.filter(site=site).first()
    if blog:
        userinfo = models.UserInfo.objects.filter(blog=blog).first()
        tag = models.Tag.objects.filter(blog=blog)
        Category = models.Category.objects.filter(blog=blog)
        date_list = models.Article.objects.raw(
            'select nid, count(nid) as num,strftime("%Y-%m",creat_time) as ctime from repository_article group by strftime("%Y-%m",creat_time)')



        if condition == 'tag':
            article_list = models.Article.objects.filter(blog=blog,tag=val)

        elif condition == 'category':
            article_list = models.Article.objects.filter(category_id=val, blog=blog).all()

        elif condition == 'data':
            article_list = models.Article.objects.filter(blog=blog).extra(
                where=['strftime("%%Y-%%m",creat_time)=%s'], params=[val, ]).all()

            print(22222,article_list)
        else:
            article_list=[]
        return render(request,
                      'home.html',
                      {
                        'userinfo': userinfo,
                       'article_list': article_list,
                       'blog': blog,
                       'tag': tag,
                       'category': Category,
                       'date_list': date_list,
                       }
                      )
    else:
        return redirect('/')

相關文章