diff --git a/gallery/templates/gallery/search.html b/gallery/templates/gallery/search.html new file mode 100644 index 0000000..878e6a7 --- /dev/null +++ b/gallery/templates/gallery/search.html @@ -0,0 +1,35 @@ +{% extends "base_dark.html" %} +{% load static %} + + +{% block title %} Gallery : Search {% endblock %} + + +{% block parallax %} + {% include "./partials/parallax.html" %} +{% endblock %} + + +{% block content %} + +
+ {% for album in results %} +
+
+
+ + {{ album.name }} + +
+
+

{{ album }}

+

{{ album.album_date|date:"d.m.Y" }} • {{ album.photos_in_album }} photos

+
+
+
+ {% endfor %} +
+ +{% include "./partials/pagination.html" %} + +{% endblock %} \ No newline at end of file diff --git a/gallery/urls.py b/gallery/urls.py index b3f6f68..042625e 100644 --- a/gallery/urls.py +++ b/gallery/urls.py @@ -11,6 +11,7 @@ urlpatterns = [ path('', views.AlbumsList.as_view(), name='main_url'), path('about/', views.About.as_view(), name='about_url'), + path('search/', views.Search.as_view(), name='search_url'), path('photostream/', views.PhotosList.as_view(), name='photos_url'), path('albums//', views.PhotoDetail.as_view(), name='photo_url'), path('albums//', views.AlbumDetail.as_view(), name='album_url'), diff --git a/gallery/views.py b/gallery/views.py index be191fe..35329a7 100644 --- a/gallery/views.py +++ b/gallery/views.py @@ -1,6 +1,7 @@ from django.conf import settings from django.core.paginator import Paginator -from django.shortcuts import get_object_or_404, redirect +from django.db.models import Q +from django.shortcuts import get_object_or_404, redirect, render from django.views.generic import DetailView, ListView, TemplateView from .models import Album, Photo, Redir @@ -97,6 +98,33 @@ class About(TemplateView): return context +class Search(TemplateView): + template_name = "gallery/search.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + query = self.request.GET.get('q', '').strip() + context['query'] = query + + results = Album.objects.none() + if query: + results = Album.objects.filter( + Q(is_public=True), + Q(name__icontains=query) | + Q(location__place__icontains=query) | + Q(location__city__name__icontains=query) + ).order_by('-album_date').distinct() + + paginator = Paginator(results, 30) + page_number = self.request.GET.get('page') + page_obj = paginator.get_page(page_number) + + context['results'] = page_obj.object_list + context['page_obj'] = page_obj + context['paginator'] = paginator + context['is_paginated'] = page_obj.has_other_pages() + return context + def redirect_to_album(request, redir_path): redir = get_object_or_404(Redir, path=redir_path) return redirect('gallery:album_url', album_slug=redir.album.slug) diff --git a/templates/base_dark.html b/templates/base_dark.html index ade49bd..356bc9c 100644 --- a/templates/base_dark.html +++ b/templates/base_dark.html @@ -36,6 +36,12 @@
+
+ +
  • About
  • {% if user.is_authenticated %} @@ -51,6 +57,11 @@
    + +