class ProfileList(ListView): template_name = 'your_template.html' model = Profile def get_queryset(self): query = self.request.GET.get('q') if query: object_list = self.model.objects.filter(name__icontains=query) else: object_list = self.model.objects.none() return object_list
Here is what the above code is Doing:
1. We created a view that inherits from ListView.
2. We defined the template_name variable to tell Django which template to use.
3. We defined the model variable to tell Django which model to query when it loads the view.
4. We created a get_queryset method to override the default queryset.
5. We created a query variable that contains the value of the q parameter in the URL.
6. If the query variable contains a value, we filter the queryset to only show objects whose name field contains the query.
7. If the query variable is empty, we return an empty queryset.