【问题标题】:How to filter posts by tags using django-taggit in django如何在 django 中使用 django-taggit 按标签过滤帖子
【发布时间】:2019-09-27 18:12:49
【问题描述】:

目前,我仍在学习 Django 并使用 Django 2.2 创建我的博客。但不知何故,当我使用 django-taggit 时出现错误。

如何通过标签过滤帖子?

我已阅读文档,但没有完全涵盖如何将其实施到实际项目中。

这是我的代码:

我尝试了几种不同的方法,但我仍在 StackOverflow 中搜索,但仍然没有答案。

/blog/tag/post-tagged 的结果与/blog 的结果相同。 那么如何从views.py 中过滤它呢?或者来自blog.html?。

所以/blog/tag/post-tagged 的结果仅来自标记的帖子。

这是我的代码:

models.py:

...

from taggit.managers import TaggableManager



"Post Model"
class Post(models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    slug = models.SlugField(max_length=100, null=True, blank=True, unique=True)
    title = models.CharField(max_length=200)

    tags = TaggableManager(blank=True)
...

views.py:

def blogIndex(request):
    posts = Post.objects.all().order_by("-created_date")[0:4]
    context = {"posts": posts,}
    return render(request, 'blog.html',  context)



def Tagging(request, slug):

    tags = Tag.objects.filter(slug=slug)
    posts = Post.objects.all().order_by("-tags")

    context = {
        'tags': tags,
        'posts': posts,
    }
    return render(request, 'blog.html', context)

urls.py:

path("tag/<slug:slug>/", views.Tagging, name='tagged'),

我的blog.html:

<div id="tags-middle">
   <div class="tags-cloud">
      Tags :
      {% for tag in post.tags.all %}
         <a href="{% url 'tagged' tag.slug %}">{{ tag.name }}</a>
      {% endfor %}
    </div>
</div>

【问题讨论】:

    标签: django django-models django-templates django-taggit


    【解决方案1】:

    在我的个人博客中,我正在执行以下操作以按标签过滤帖子:

    urls.py:

    ...
    path('tag/<tag>', views.tag, name='tag'),
    ...
    

    views.py:

    from django.shortcuts import render    
    ...
    def tag(request, tag):
        "This page shows all posts with the related tag"
        
         posts = Post.objects.filter(tags__name=tag)
         return render(request, 'blog/tag.html', {'tag': tag, 'posts': posts})
        
    

    有了这个,你可以有一个blog/tag.html 模板,比如:

    <h2>List of posts of the tag "{{ tag }}"</h2>
    
    {% if posts %}
    
    <ul>
    
    {% for post in posts %}
    
    // displays whatever you want about the post
    
    {% endfor %}
    
    </ul>
    
    {% else %}
    
    <p>No post related to that tag yet.</p>
    
    {% endif%}
    

    【讨论】:

      【解决方案2】:

      我已经通过过滤来自views.py 的标签解决了这个问题。

      因为我的帖子中有多个标签。所以__intags 必须在列表中

      这是我的views.py

      tags = Tag.objects.filter(slug=slug).values_list('name', flat=True)
      posts = Post.objects.filter(tags__name__in=tags)
      

      基本上,正如documentation 所说,我们可以使用字符串进行过滤,就像:

      posts = Post.objects.filter(tags__name__in=["Lorem"])
      

      但它只需要一个字符串。

      如果我尝试使用多个字符串,例如 ["Lorem", "Ipsum"],它只会在 /blog/tag/lorem 上显示一个空白页。

      【讨论】:

        猜你喜欢
        • 2018-12-05
        • 1970-01-01
        • 1970-01-01
        • 2020-05-29
        • 1970-01-01
        • 1970-01-01
        • 2020-04-22
        • 1970-01-01
        • 2016-12-24
        相关资源
        最近更新 更多