我想知道如何过滤标签。
我的意思是:如果点击此标记,则显示所有拥有此标记的文章。 (django - 1.7.4和python 2.7.9)
views.py
def tag(request):
context = {}
populateContext(request, context)
return render_to_response('ajouter.html', context, Context({'tout_tags': Article.tags.all()}))
def innertag(request, id):
context = {}
populateContext(request, context)
return render_to_response('innerajouter.html', context, Context({'tag': get_object_or_404(Article.tags, id=id)}))
ajouter.html
{% for tag in tout_tags %}
<a href="{% url "article.views.innertag" tag.id %}">{{ tag.name }}</a>
{% endfor %}
innerajouter.html
<h3>For the tag: {{ tag.name }}</h3>
//How can I filter ? To get only the selectioned tags ?
这是我的项目视图:
答案 0 :(得分:0)
如果您想要显示某个标签下的所有文章......
from django.template import RequestContext
def displayAllArticlesUnderTage(request, tagID):
context = RequestContext(request)
tag = tag.objects.get(id = tagID)
#Note, I am not sure of your database relationship between the tags and
#the articles so you will probably have to change the next line...
articles = Article.objects.filter(tag = tag)
context_dict = { "articles" : articles, "tag" : tag }
return render_to_response([your template name], context_dict, context)
我推测你的文章与标签对象的关系。如果这是错的,请告诉我。
我的回答取决于文章的展示方式。我只是在这里给出一般语法......
{% for article in articles %}
<h1>{{ article.titre }}</h1>
<h5>{{ article.autuer }}</h5>
<p>{{ article.contre }}</p>
*Whatever other things you need from each article*
{% endfor &}