如何为django-taggit创建列表和详细视图? [英] How do I create list and detail views for django-taggit?

查看:111
本文介绍了如何为django-taggit创建列表和详细视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的模型,使用Django Taggit进行标记。



一切都很好,但现在我想扩展一些功能,我是一个有点困惑。



我想要的是两个视图。



一个显示系统中的所有标签。
从我的应用程序中显示所有内容的特定标签。



对于每个视图,对我来说都是有意义的。 >

在views.py for myapp


  1. 所有标签



    from myapp.models import应用程序



    from taggit.models import标签



    class TagList(ListView):



    获取db中的所有标签

      queryset = Tag.objects.all()
    template_name =myapp / TagList.html
    paginate_by = 10


  2. 来自myapp.models导入应用程序的标签的所有内容



    from taggit.models import Tag



    class TaggedList(ListView):
    获取所有contet一个标签

      template_name =myapp / TaggedList.html

    def get_object自我):
    return get_list_or_404(App,tag__iexact = self.kwargs ['tag'])


我失去了思想,还是真的那么容易? BTW,我使用泛型类视图。



感谢您的帮助。
Dave

解决方案

2。
我相信这是为了返回单个对象,而不是多个对象。

  def get_object(self):

相反,您应该尝试以下内容:

  def get_queryset(self):
return TaggedItem.objects.filter(tag__iexact = self.kwargs ['tag'])

这将返回GenericForeignKeys的项目列表



如果您只对特定模型感兴趣称为App然后

  return App.objects.filter(tags__name__in = [self.kwargs ['tag']])

模板中的默认变量名称为TaggedItem_list,然后

  {%for TaggedItem_list%} 
{{item.content_object}} {#generic foreign key here#}
{%endfor%}

urls.py必须类似于

  URL(r'someapp /(P<标签> \w +) / $',TaggedList.as_view())


I have a fairly simple model that uses Django Taggit for tagging.

Everything works great, but now I'd like to expand some functionality and I'm a little confused.

What I want is two views.

One that shows all my tags in the system. One that shows all the content from my app with a specific tag.

What makes sense to me is to do the following for each view.

in views.py for myapp

  1. All Tags

    from myapp.models import App

    from taggit.models import Tag

    class TagList(ListView):

    """ Get all the tags in the db """

    queryset = Tag.objects.all()
    template_name = "myapp/TagList.html"
    paginate_by = 10
    

  2. All content for a Tag

    from myapp.models import App

    from taggit.models import Tag

    class TaggedList(ListView): """ Get all the contet for a tag """

    template_name = "myapp/TaggedList.html"
    
    def get_object(self):
        return get_list_or_404(App, tag__iexact=self.kwargs['tag'])
    

Have I lost my mind or is it really that easy? BTW, I'm using generic class views.

Thanks for the help. Dave

解决方案

2. I believe this is for returning a single object, not multiple objects.

def get_object(self):  

Instead perhaps you should try something like the following:

def get_queryset(self):
    return TaggedItem.objects.filter(tag__iexact=self.kwargs['tag'])

This returns a list of items with GenericForeignKeys

If you are only interested in a specific model called App then

    return App.objects.filter(tags__name__in=[self.kwargs['tag']])

Default variable name in the template is TaggedItem_list then

{% for item in TaggedItem_list %}
   {{item.content_object}} {# generic foreign key here #}
{% endfor %}

The urls.py would have to be similar to

url(r'someapp/(?P<tag>\w+)/$', TaggedList.as_view())

这篇关于如何为django-taggit创建列表和详细视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆