Reputation: 418
I am trying to achive some basic search in my search template, but when I search, it works in the URL, but doesn't change any of the posts in the template.
Views:
class SearchListView(ListView):
model = Post
template_name = "public/search.html"
def get_context_data(self, **kwargs):
context = super(SearchListView, self).get_context_data(**kwargs)
context['queryset'] = Post.objects.filter(live=True)
context['category'] = Category.objects.all()
return context
def SearchListView(request):
query = self.request.GET.get("q")
if query:
queryset = queryset.filter(title_icontains=query)
I am not sure if this is the right way to set things up as the tutorial I was watching was not using class based views.
HTML:
<div class="container mt-5 mb-5">
<div class="banner-search-main mb-5">
<form method='GET' action=''>
<p class="text-muted">Keywords:</p>
<input type="text" name='q' class="homebanner-search" placeholder="Enter your keywords" value='{{ request.get.q }}'>
<input type="submit" value="search">
</form>
</div>
<div class="detail-container">
{% for post in queryset %}
<div class="col-sm card-container">
<a href="{% url 'post-detail' post.slug %}">
<div class="main-card">
<div class="main-card-img">
<img src="https://via.placeholder.com/270x150" class="card-img-top" alt="#">
</div>
<div class="main-card-body">
<p class="featured-category category-{{ post.category.colorcode }}">{{ post.category }}</p>
<p class="featured-title">{{ post.title }}</p>
<div class="featured-authcat">
<p class="featured-author mr-3"><i class="fas fa-user mr-1"></i>{{ post.author|title }}</p>
</div>
<p class="featured-subtitle">{{ post.sub_description|truncatewords:25 }}</p>
</div>
</div>
</a>
</div>
{% endfor %}
</div>
</div>
Upvotes: 1
Views: 1095
Reputation: 985
You forgot self as the first parameter in your searchListView
def SearchListView(self, request):
query = self.request.GET.get("q")
if query:
queryset = queryset.filter(title_icontains=query)
and for a better pythonic pep-8 style,The function name should be search_list_view
.
check pep-8 for Method Names and Instance Variables
def search_list_view:
....
and for what you are trying to accomplish it's better override get_queryset
from the ListView.
def get_context_data(self, **kwargs):
context = super(SearchListView, self).get_context_data(**kwargs)
context['queryset'] = self.get_queryset()
context['category'] = Category.objects.all()
return context
def get_queryset(self):
queryset = super(SearchListView, self).get_queryset().filter(live=True)
query = self.request.GET.get("q")
if query:
queryset = queryset.filter(title__icontains=query)
return queryset
Upvotes: 0
Reputation: 2439
your ListView with get_queryset method
class SearchListView(ListView):
model = Post
template_name = "public/search.html"
def get_queryset(self):
queryset = super().get_queryset().filter(live=True)
query = self.request.GET.get("q")
if query:
queryset = queryset.filter(title__icontains=query)
return queryset
def get_context_data(self, **kwargs):
context = super(SearchListView, self).get_context_data(**kwargs)
context['queryset'] = self.get_queryset()
context['category'] = Category.objects.all()
return context
Upvotes: 2
Reputation: 7049
Your issue is that you created a random function in your class which does not override anything. What you are trying to do is override get_queryset()
.
class SearchListView(ListView):
model = Post
template_name = "public/search.html"
def get_context_data(self, **kwargs):
context = super(SearchListView, self).get_context_data(**kwargs)
context['queryset'] = self.get_queryset()
context['category'] = Category.objects.all()
return context
def get_queryset(self):
queryset = super().get_queryset().filter(live=True)
query = self.request.GET.get("q")
if query:
queryset = queryset.filter(title__icontains=query)
return queryset
Upvotes: 0
Reputation: 4664
First of all, I would suggest visiting this to view the infrastructure of class-based views.
class SearchListView(ListView):
model = Post
template_name = "public/search.html"
success_url ='where you want to redirect'
def get_queryset(self): # method that returns the queryset is `get_queryset`
query = self.request.GET.get("q")
if query:
queryset = self.model.objects.filter(title__icontains=query) # double underscore not single
return queryset
def get_context_data(self, **kwargs):
context = super(SearchListView, self).get_context_data(**kwargs)
context['queryset'] = self.get_queryset()
context['category'] = Category.objects.all()
return context
Upvotes: 0