add search

This commit is contained in:
Christoph J. Scherr 2023-10-07 15:26:36 +02:00
parent 06e3172234
commit 559eb3f9fa
2 changed files with 29 additions and 3 deletions

View File

@ -17,7 +17,11 @@
<div class="container-fluid"> <div class="container-fluid">
<div class="row mb-5"> <div class="row mb-5">
<div class="col col-xxl mb-5" id="headline"> <div class="col col-xxl mb-5" id="headline">
<h1 class="display-1">Browse</h1> <a class="text-reset link-offset-2 link-underline link-underline-opacity-0"
href="{% url "blog:browse" %}"
style="display: inline-block;">
<h1 class="display-1 w-0">Browse</h1>
</a>
{# center headline on small screens #} {# center headline on small screens #}
<script> <script>
if (screen.width < 480) { if (screen.width < 480) {
@ -63,6 +67,8 @@
<div class="row mb-5"> <div class="row mb-5">
<div class="container-fluid p-0 w-100 h-100"> <div class="container-fluid p-0 w-100 h-100">
<div class="row gap-3"> <div class="row gap-3">
{# TODO: display some info if the queryset is empty #}
{# TODO: pagination #}
{% for post in posts %} {% for post in posts %}
<div class="card col mx-auto my-2" style="min-width: 200px;"> <div class="card col mx-auto my-2" style="min-width: 200px;">
<a class="text-reset link-offset-2 link-underline link-underline-opacity-0" <a class="text-reset link-offset-2 link-underline link-underline-opacity-0"

View File

@ -1,6 +1,7 @@
from django.shortcuts import Http404, HttpResponse, get_object_or_404, render from django.shortcuts import Http404, HttpResponse, get_object_or_404, render
from django.utils.translation import get_language from django.utils.translation import get_language
from django.views.generic import TemplateView, DetailView, ListView, View from django.views.generic import TemplateView, DetailView, ListView, View
from django.db.models import Q
from .models import BlogPost, Category from .models import BlogPost, Category
from start.views import SearchableView from start.views import SearchableView
@ -58,8 +59,27 @@ class Browse(ListView):
if "category" in self.request.GET and len( if "category" in self.request.GET and len(
self.request.GET["category"].strip()) > 0: self.request.GET["category"].strip()) > 0:
category = self.request.GET["category"] category = self.request.GET["category"]
category = Category.objects.get(slug=category) try:
objects = objects.filter(category=category) category = Category.objects.get(slug=category)
objects = objects.filter(category=category)
except Category.DoesNotExist:
objects = objects.none()
if "search" in self.request.GET and len(
self.request.GET["search"].strip()) > 0:
search = self.request.GET["search"]
# __icontains matches those attributes that contain the
# search string without caring about lower/upper cases
objects = objects.filter(
Q(title_en__icontains=search) |
Q(title_de__icontains=search) |
Q(subtitle_en__icontains=search) |
Q(subtitle_de__icontains=search) |
Q(desc_en__icontains=search) |
Q(desc_de__icontains=search) |
# Q(body_en__icontains=search) |
# Q(body_de__icontains=search) |
Q(slug__icontains=search)
)
return objects return objects
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):