I am trying to filter my 'Posts' model by a parameter passed into a query string in the url. If this parameter is not given, I want the url to just return all posts.
To my (obviously incorrect) understanding, the following is how it should work:
urls.py:
path('feed?category=<str:category>', views.Posts.as_view())
views.py:
def Posts(APIView):
def get(self, request, **kwargs):
if self.kwargs['category']:
queryset = Post.objects.filter(category_name = self.kwargs['category'])
else:
queryset = Post.objects.all()
With the above code, I would expect that by requesting a url .../feed, the view would set the queryset to all posts, however if I entered a url .../feed?category=x it would only return posts of this category.
Currently, the program does not function as such. How would I accomplish this functionality?
Thanks, Grae
pathonly checks the path of the URL, the part after the?is not the path, that is the query string, so you can not filter with that in the path.