From a brief glance at the RTD (and assuming you are using CBV's), it looks like you should create a standard ListView and then override get_queryset() with the extra filter to narrow down the list of models retrieved:
http://django-taggit.readthedocs.org/en/latest/api.html#filtering
You can use a (?P<tag_slug>[\w-]+) or whatever you want to name it to capture a URL parameter specifying the desired tag, and pull that parameter when adding the filter in get_queryset().
Something like this:
def get_queryset(self):
qs = super(ListViewName, self).get_queryset()
return qs.filter(tags__name=self.kwargs.get('tag_slug', None))
That works for filtering via a single tag (which sounds like what you are trying to do). A non-matching slug will return an empty list, and a missing slug will return all of the models without tags (I presume). For multiple tags, you should either accept GET or POST parameters from a form submission, unless you come up with some funky regex to capture multiple tags via the URL (not recommended), and modify the get_queryset() call to use __in rather than =.
There shouldn't be any need to pass extra parameters to the view call. A typical ListViewName.as_view() should suffice. Everything you need should be gathered via a form submission or a URL kwarg.
Full disclosure, I haven't worked with django-taggit at all, so I could be completely wrong.
For completeness in my answer, in the event that I'm wrong, you can allow passing of extra arguments to the as_view() call by overriding the __init__() method of the view and adding in the extra arguments to the overridden method:
def __init__(self, request, qs, **kwargs):
super(ListViewName, self)__init__(**kwargs) # call the regular __init__() with the originally expected kwargs, notice that request and qs are missing
self.request = request #is this the view request? It should already be available via self.request
self.qs = qs
You may also want to look at django-filter or django-easyfilter to provide a small form to allow users to search for tags, if needed.
HTH,
-James
--Hola,I was planning on using Django taggit for tagging models.
http://django-taggit.readthedocs.org
https://github.com/alex/django-taggitI am trying to get some sort of CBV (eg lListView of model's with tag x) happening but it doesn't seem able to do this.has a tagged_object_list function, but I can't seem to get it working and can't find a test that might give me an indication.So I tried re-creating it in my own views.py and y equivalent to this line:
return ListView.as_view(request, qs, **kwargs)keeps throwing errors like "ListView.as_view takes exactly 1 argument"And now I'm confused - this app is by Alex Gaynor, so I presume it's solid and I'm the one making the mistake.Can anyone give me an indication how either the taggit.views might be used or how I might implement something like ListView.as_view(request, qs, **kwargs)CheersL.------
let's build quiet armies friends, let's march on their glass towers...let's build fallen cathedrals and make impractical plans
- GYBE
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGBeqiPoEeEbpETfAmHq%3DAA2WkEHHbGO79NapXf8YAjA5y8nZQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXG_-WqbJ9MiAJAqRLGCJex9Db0bTJ0N2nH-Zv%3DmEJ5GA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment