1👍
✅
views.py
...
queryset_list = Post.objects.all().order_by("-published")
if request.GET.get('cat'):
queryset_list = queryset_list.filter(category__slug=request.GET.get('cat'))
...
urls.py
from .views import (
post_list,
post_detail
)
urlpatterns = [
# Add name to url so that we can use it with reverse
url(r'^$', post_list, name='list'),
url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
]
models.py
class Category(models.Model):
...
def get_absolute_url(self):
return reverse('posts:list')+'?cat='+self.slug
Source:stackexchange.com