Here's my code in url.py:
(r'^tag/(?P<tag>\w+)/$',
ListView.as_view(
model=List,
context_object_name='some_list',
queryset=List.objects.filter(tag__name__in=[tag_name]),
template_name='some_list.html'))
I'd like to pass (?P<tag>\w+)
to "tag_name
" filter, but I don't know how to do it.
Also how can I pass multiple tags? Like this:
http://www.mysite.com/tag/tag1+tag2+tag3
url.py should get "tag1+tag2+tag3
", split it into "tag1
", "tag2
" and "tag3
", and then put them in "tag__name__in
":
queryset=List.objects.filter(tag__name__in=[tag1, tag2, tag3])
Basically I'm confused by the class-based generic view. Any idea?
You can overwrite the view's get_queryset
method and construct a queryset with your results, eg.
from django.views.generic.list import ListView
class MyList(ListView):
def get_queryset(self):
tag_list = self.kwargs['tags'].split('+')
return List.objects.filter(tag__name__in=tag_list)
# urls.py
...
url(r'tag/(?<tags>[\w\+]+)/', MyList.as_view())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With