如何使用taggit在django博客中添加标签

时间:2018-12-03 21:15:50

标签: javascript python html css django

am试图在我的post_list函数中添加一些标签,我已经安装了taggit并在外壳中对其进行了测试,它工作正常,并且能够在html模板blog.html中显示标签,但是问题是试图添加url特定标签显示错误.NoReverseMatch在/ blog / 找不到带有参数'('python',)'的'post_list'。尝试了1种模式:['blog \ / $']

我有一个名为python的博客类别,这是到目前为止我的作品

views.py

def post_list(request,tag_slug=None):
    object_list=Post.objects.filter(status='Published').order_by("-created")
    recent_post=object_list[:6]
    categories = Category.objects.all().annotate(post_count=Count('post'))
    for category in categories:
        print(category.post_count)
    if tag_slug:

       tag = get_object_or_404(Tag, slug=tag_slug)

       object_list = object_list.filter(tags__in=[tag])
    page = request.GET.get('page', 1)
    paginator = Paginator(object_list, 3)
    try:
        items = paginator.page(page)
    except PageNotAnInteger:
        items = paginator.page(1)
    except EmptyPage:
        items = paginator.page(paginator.num_pages)
    context={
        'items':items,
        'recent_post':recent_post,
        'categories':categories,
    }
    return render(request,"blog.html",context)

blog.html

<div class="widget">
          <h3 class="badge">TAGS</h3>
          <div class="tagcloud">
{% for obj in items %}
  {% for tag in obj.tags.all %}
    <a href="{% url 'post_list' tag.slug %} ">{{ tag.name }}</a>

  {% if not forloop.last %}, {% endif %}

  {% endfor %}
{% endfor %}
          </div>
        </div>

请帮助

0 个答案:

没有答案