Forums

Set filter to queryset by url parameter

Hello . I want to make filter by url parameter as like section=<section> instead of static value section='Politics' . Here is my url patterns :

urlpatterns = [url(r'^(?P<section>\w+)$',
ListView.as_view(queryset=New.objects.filter(section='Politics').order_by("date")[:25],
                                            template_name="news/news.html"))]

How can adjust it ?

[edit by admin: formatting]

I think the best way to do that in Django is to create your own subclass of ListView. Check out this page of the Django docs for a worked example.

Thanks for the link