Add a search form to the index

This commit is contained in:
Christian Lawson-Perfect 2025-05-04 08:22:31 +01:00
parent 4f92cbf450
commit c12f637d17
3 changed files with 19 additions and 1 deletions

View file

@ -7,6 +7,15 @@
{% endblock header %} {% endblock header %}
{% block main %} {% block main %}
<form id="search" method="GET" action="{% url 'search' %}">
<label for="query">Search</label>
<input type="search" id="query" name="query" list="slugs">
<datalist id="slugs">
{% for think in thinks %}<option value="{{think.slug}}"/>{% endfor %}
</datalist>
</form>
<section id="templates"> <section id="templates">
<h2>Templates</h2> <h2>Templates</h2>
<ul id="templates-list"> <ul id="templates-list">

View file

@ -18,5 +18,6 @@ urlpatterns = [
path('think/<slug:slug>/jj/commit', JJCommitView.as_view(), name='jj_commit'), path('think/<slug:slug>/jj/commit', JJCommitView.as_view(), name='jj_commit'),
path('new', CreateThinkView.as_view(), name='new_think'), path('new', CreateThinkView.as_view(), name='new_think'),
path('new/<slug:slug>', RemixThinkView.as_view(), name='remix_think'), path('new/<slug:slug>', RemixThinkView.as_view(), name='remix_think'),
path('search', search, name='search'),
] ]

View file

@ -4,6 +4,7 @@ from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.views import generic from django.views import generic
from django.urls import reverse from django.urls import reverse
from django.utils.timezone import now
from itertools import groupby from itertools import groupby
import json import json
import mimetypes import mimetypes
@ -32,6 +33,10 @@ class ThinkMixin(LoginRequiredMixin):
model = Think model = Think
context_object_name = 'think' context_object_name = 'think'
def search(request, *args, **kwargs):
query = request.GET.get('query','')
think = Think.objects.get(slug__icontains=query)
return redirect(think.get_absolute_url())
class IndexView(ThinkMixin, generic.ListView): class IndexView(ThinkMixin, generic.ListView):
template_name = 'thinks/index.html' template_name = 'thinks/index.html'
@ -41,7 +46,7 @@ class IndexView(ThinkMixin, generic.ListView):
context['templates'] = Think.objects.filter(is_template=True) context['templates'] = Think.objects.filter(is_template=True)
context['recent_thinks'] = Think.objects.filter(is_template=False).order_by('-creation_time')[:3] context['recent_thinks'] = Think.objects.all().order_by('-last_edited', '-creation_time')[:3]
context['thinks'] = sorted(Think.objects.filter(is_template=False), key=lambda t: (t.category if t.category else '', -t.creation_time.timestamp())) context['thinks'] = sorted(Think.objects.filter(is_template=False), key=lambda t: (t.category if t.category else '', -t.creation_time.timestamp()))
@ -100,6 +105,9 @@ class ThinkView(ThinkMixin, generic.DetailView):
think = self.object think = self.object
think.last_edited = now()
think.save(update_fields=('last_edited',))
root = think.root root = think.root
strpath = self.request.GET.get('path') strpath = self.request.GET.get('path')