0

My views.py

class SearchView(TemplateView):
    template_name = 'search.html'

    def get(self, request, *args, **kwargs):
        q = request.GET.get('q', '')
        self.results = Item.objects.filter(title__icontains=q)
        return super().get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        return super().get_context_data(results=self.results, **kwargs)

My urls.py

url(r'^search/$', SearchView.as_view(), name='search')

My search.html

{% extends 'base.html' %} {% load static %} {% block content %}

<body>
    <h1>Search Result</h1>
    <ul>
        {% for item in q %}
        <li>
            {{ q.title }}, {{ q.price }}
        </li>
        {% endfor %}
    </ul>

</body>
{% endblock%}}

My nav.html

<form method="GET" action="{% url 'core:search' %}">

This is the code that i used but due to some missing or error in this above code i can't get any data if i make any search in my website, Can some one please tell me what is the mistake i have done. Thank you.

My Models.py

class Item(models.Model):
    title = models.CharField(max_length=50)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    model_no = models.CharField(max_length=100)
6
  • I think you should use {{ item.title }}, {{ item.price }} in the template instead of {{ q.title }}, {{ q.price }} Commented Jan 28, 2021 at 7:24
  • I already tried it but it is not working Commented Jan 28, 2021 at 7:57
  • would you provide models.py Commented Jan 28, 2021 at 8:01
  • My Models.py code class Item(models.Model): title = models.CharField(max_length=50) category = models.ForeignKey(Category, on_delete=models.CASCADE) model_no = models.CharField(max_length=100) Commented Jan 28, 2021 at 8:20
  • why are you using products in this line: {% for item in products %}, where is it coming from? Commented Jan 28, 2021 at 8:36

1 Answer 1

1

try this:

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)
        context['results'] = self.results
        return context

in html

{% extends 'base.html' %} 
{% load static %} 

{% block content %}

<body>
    <h1>Search Result</h1>
    <ul>
        {% for item in results %}
        <li>
            {{ item.title }}, {{ item.price }}
        </li>
        {% endfor %}
    </ul>

</body>
{% endblock%}}
Sign up to request clarification or add additional context in comments.

7 Comments

should i change any thing in my urls.py or not.
write this: url('search/', SearchView.as_view(), name='search') You have to get your "q" in get() function from somewhere, so that the url must be something like 'search/?q=abc'.
I tried to execute the changed code but it is showing error at context in views.py
Name Error: name 'context' is not defined
forgot a line: context = super().get_context_data(**kwargs), added in answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.