首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django-tagging:通过指定多个标签来抓取对象?

Django-tagging:通过指定多个标签来抓取对象?
EN

Stack Overflow用户
提问于 2010-06-20 05:57:17
回答 1查看 651关注 0票数 1

我目前在urls.py中有一个条目,它为我的bug获取单独的永久链接:

代码语言:javascript
复制
from django.conf.urls.defaults import *

from tagging.views import tagged_object_list

from bugs.models import Bug

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^workarounds/', include('workarounds.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
    # to INSTALLED_APPS to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    (r'^$', 'django.views.generic.simple.direct_to_template', {'template':'homepage.html'}),

    (r'^bugs/(?P<slug>[-\w]+)/$', 'bugs.views.bug_detail'),
    (r'^bugs/tagged/(?P<tag>[^/]+)/$', 
    'tagging.views.tagged_object_list',
    {
        'queryset_or_model': Bug,
        'template_name' : 'tag/lone.html'}),
    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
)

因此,如果我指定一个url,比如说bugs/tagged/firefox,它会显示出火狐标签。如何让它通过多个标签过滤出来?例如:firefox+css将返回所有带有firefoxcss标签的对象。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-21 22:16:56

您必须构建自己的视图,而不是使用tagging.views.tagged_object_list

代码语言:javascript
复制
(r'^bugs/tagged/(?P<tags>[-\w+]+)/$', your_tag_view)

在您的视图中,获取要搜索的标签的列表:

代码语言:javascript
复制
tags = tags.split('+')

然后,使用TaggedItem.objects.get_by_model查询,它可以方便地接受标记列表:

代码语言:javascript
复制
from tagging.models import TaggedItem
bugs = TaggedItem.objects.get_by_model(Bug, tags)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3077385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档