代码之家  ›  专栏  ›  技术社区  ›  meder omuraliev

Django标记:通过指定多个标记来抓取对象?

  •  1
  • meder omuraliev  · 技术社区  · 14 年前

    我目前在 urls.py 它为我的虫子带来了孤独的永久链接:

    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 将返回所有标记为 firefox css .

    1 回复  |  直到 14 年前
        1
  •  3
  •   Chris Lawlor    12 年前

    tagging.views.tagged_object_list .

    (r'^bugs/tagged/(?P<tags>[-\w+]+)/$', your_tag_view)
    

    tags = tags.split('+')
    

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

    from tagging.models import TaggedItem
    bugs = TaggedItem.objects.get_by_model(Bug, tags)
    
    推荐文章