nasc
nasc

Reputation: 299

FieldError at / Cannot resolve keyword 'title_icontains' into field. Choices are: complete, create, decription, id, title, user, user_id

I'm getting an error while making the search functionality for my to-do list in Django. the error

I was following dennis ivy's tutorial, did the same thing what he did but I'm still getting the error. enter image description here

My code:

class TaskList(LoginRequiredMixin, ListView):
    model = Task
    context_object_name = 'tasks'

    # For each user
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['tasks'] = context['tasks'].filter(user= self.request.user)
        context['count'] = context['tasks'].filter(complete = False).count()

        search_input = self.request.GET.get('search-area') or ''

        if search_input:
            context['tasks'] = context['tasks'].filter(
                title_icontains = search_input)  # <= Here I get the error
        context['search_input'] = search_input
        return context

What is going wrong?

Upvotes: 0

Views: 1308

Answers (1)

Neeraj
Neeraj

Reputation: 997

title_icontains acts like normal variable, whereas "__" double underscores are used for lookups, field relations etc.

context['tasks'] = context['tasks'].filter(
                title__icontains = search_input)

Please refer : Making Queries- Django Doc

Upvotes: 1

Related Questions