Add search
This commit is contained in:
parent
333ed61b6e
commit
f3ec438305
4 changed files with 76 additions and 1 deletions
35
gallery/templates/gallery/search.html
Normal file
35
gallery/templates/gallery/search.html
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
{% extends "base_dark.html" %}
|
||||||
|
{% load static %}
|
||||||
|
|
||||||
|
<!-- Title -->
|
||||||
|
{% block title %} Gallery : Search {% endblock %}
|
||||||
|
|
||||||
|
<!-- Parallax -->
|
||||||
|
{% block parallax %}
|
||||||
|
{% include "./partials/parallax.html" %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
<!-- Content -->
|
||||||
|
{% block content %}
|
||||||
|
|
||||||
|
<div class="uk-grid-small uk-child-width-1-2@s uk-child-width-1-3@m" uk-grid="masonry: true">
|
||||||
|
{% for album in results %}
|
||||||
|
<div>
|
||||||
|
<div class="uk-card uk-card-default">
|
||||||
|
<div class="uk-card-media-top">
|
||||||
|
<a href="{{ album.get_absolute_url }}">
|
||||||
|
<img src="{{ album.cover_url }}" alt="{{ album.name }}">
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
<div class="uk-overlay uk-overlay-primary uk-position-bottom uk-padding-small">
|
||||||
|
<h3 class="uk-card-title"><a href="{{ album.get_absolute_url }}">{{ album }}</a></h3>
|
||||||
|
<p>{{ album.album_date|date:"d.m.Y" }} • {{ album.photos_in_album }} photos</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% include "./partials/pagination.html" %}
|
||||||
|
|
||||||
|
{% endblock %}
|
|
@ -11,6 +11,7 @@ urlpatterns = [
|
||||||
|
|
||||||
path('', views.AlbumsList.as_view(), name='main_url'),
|
path('', views.AlbumsList.as_view(), name='main_url'),
|
||||||
path('about/', views.About.as_view(), name='about_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('photostream/', views.PhotosList.as_view(), name='photos_url'),
|
||||||
path('albums/<path:album_slug>/<filename:photo_slug>', views.PhotoDetail.as_view(), name='photo_url'),
|
path('albums/<path:album_slug>/<filename:photo_slug>', views.PhotoDetail.as_view(), name='photo_url'),
|
||||||
path('albums/<path:album_slug>/', views.AlbumDetail.as_view(), name='album_url'),
|
path('albums/<path:album_slug>/', views.AlbumDetail.as_view(), name='album_url'),
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.paginator import Paginator
|
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 django.views.generic import DetailView, ListView, TemplateView
|
||||||
|
|
||||||
from .models import Album, Photo, Redir
|
from .models import Album, Photo, Redir
|
||||||
|
@ -97,6 +98,33 @@ class About(TemplateView):
|
||||||
return context
|
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):
|
def redirect_to_album(request, redir_path):
|
||||||
redir = get_object_or_404(Redir, path=redir_path)
|
redir = get_object_or_404(Redir, path=redir_path)
|
||||||
return redirect('gallery:album_url', album_slug=redir.album.slug)
|
return redirect('gallery:album_url', album_slug=redir.album.slug)
|
||||||
|
|
|
@ -36,6 +36,12 @@
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div class="uk-navbar-right uk-margin-right">
|
<div class="uk-navbar-right uk-margin-right">
|
||||||
|
<div class="uk-navbar-item uk-visible@m">
|
||||||
|
<form class="uk-search uk-search-default" action="{% url 'gallery:search_url' %}" method="get">
|
||||||
|
<a href="" class="uk-search-icon-flip" uk-search-icon></a>
|
||||||
|
<input id="q" name="q" class="uk-search-input uk-form-width-small" type="search" placeholder="Search albums" aria-label="Search">
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
<ul class="uk-navbar-nav">
|
<ul class="uk-navbar-nav">
|
||||||
<li class="uk-visible@m"><a href="{% url 'gallery:about_url' %}">About</a></li>
|
<li class="uk-visible@m"><a href="{% url 'gallery:about_url' %}">About</a></li>
|
||||||
{% if user.is_authenticated %}
|
{% if user.is_authenticated %}
|
||||||
|
@ -51,6 +57,11 @@
|
||||||
<div id="offcanvas-nav" uk-offcanvas="flip: true; overlay: true">
|
<div id="offcanvas-nav" uk-offcanvas="flip: true; overlay: true">
|
||||||
<div class="uk-offcanvas-bar">
|
<div class="uk-offcanvas-bar">
|
||||||
<button class="uk-offcanvas-close" type="button" uk-close></button>
|
<button class="uk-offcanvas-close" type="button" uk-close></button>
|
||||||
|
<!-- MOBILE HAKU -->
|
||||||
|
<form class="uk-search uk-search-default uk-margin-small-bottom" action="{% url 'gallery:search_url' %}" method="get">
|
||||||
|
<a href="" class="uk-search-icon-flip" uk-search-icon></a>
|
||||||
|
<input id="q-mobile" name="q" class="uk-search-input" type="search" placeholder="Search albums" aria-label="Search">
|
||||||
|
</form>
|
||||||
<ul class="uk-nav uk-nav-default">
|
<ul class="uk-nav uk-nav-default">
|
||||||
<li><a href="{% url 'gallery:main_url' %}">Home</a></li>
|
<li><a href="{% url 'gallery:main_url' %}">Home</a></li>
|
||||||
<li><a href="{% url 'gallery:albums_url' %}">Albums</a></li>
|
<li><a href="{% url 'gallery:albums_url' %}">Albums</a></li>
|
||||||
|
|
Loading…
Add table
Reference in a new issue