单击时 Django 获取与标签关联的内容
Django get content associated to the tag when clicked
我想知道如何过滤标签。
我的意思是:如果点击这个标签,显示所有有这个标签的文章。
(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 %}
内ajouter.html
<h3>For the tag: {{ tag.name }}</h3>
//How can I filter ? To get only the selectioned tags ?
这是我的项目的视图:
如果您想显示某个标签下的所有文章...
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 &}
我想知道如何过滤标签。
我的意思是:如果点击这个标签,显示所有有这个标签的文章。 (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 %}
内ajouter.html
<h3>For the tag: {{ tag.name }}</h3>
//How can I filter ? To get only the selectioned tags ?
这是我的项目的视图:
如果您想显示某个标签下的所有文章...
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 &}