Django标签过滤问题

时间:2015-03-08 11:43:59

标签: python django tags django-filter django-taggit

我的代码存在问题,我想过滤代码,只显示我的文章是否有某个代码。

这是我的代码: 的 views.py

from article.models import Article

def innertag(request, id):
    context = {}
    populateContext(request, context)
    return render_to_response('innerajouter.html', context, Context({"articles" : Article.objects.filter(tags__name=Article.tags), "tag" : get_object_or_404(Article.tags, id=id)}))

"articles" : Article.objects.filter(tags__name=Article.tags)

我的代码的这部分是问题

它给我这个错误消息: / article / tags / 2处的StopIteration没有提供异常消息


innerajouter.html

<h3>For the tag: {{ tag.name }}</h3>

{% for article in articles %}
    <h1>{{ article.titre }}</h1>
    <h5>{{ article.auteur }}</h5>
    <p>{{ article.contenu }}</p>
{% endfor %}

from django.db import models
from taggit.managers import TaggableManager

class Article(models.Model):
    titre = models.CharField(max_length=100)
    auteur = models.CharField(max_length=42)
    contenu = models.TextField(null=True)
    tags = TaggableManager()

    def __str__(self):
        return self.titre

我该如何解决?


尝试解决方案:

"articles" : Article.objects.filter(tags=Article.tags)

/ article / tags / 2中的TypeError int()参数必须是字符串或数字,而不是&#39; _TaggableManager&#39;

1 个答案:

答案 0 :(得分:1)

你应该找到具体的标签,然后通过它过滤文章:

from django.shortcuts import get_object_or_404, render
from taggit.models import Tag

def innertag(request, id)
    tag = get_object_or_404(Tag, id=id)
    articles = Article.objects.filter(tags=tag)
    context = {'tag': tag, 'articles': articles}
    populateContext(request, context)
    return render(request, 'innerajouter.html', context)