Updated search view to process post via class-based view

This commit is contained in:
Edgar P. Burkhart 2022-12-28 12:33:18 +01:00
parent a6805212fc
commit 0d37594f0b
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
4 changed files with 26 additions and 24 deletions

View file

@ -48,7 +48,7 @@
accesskey="s"> accesskey="s">
{% translate "Snapshot" %} {% translate "Snapshot" %}
</a> </a>
<form id="search" action="{% url 'search_post' %}" method="post"> <form id="search" action="{% url 'search' %}" method="post">
{% csrf_token %} {% csrf_token %}
<input type="text" <input type="text"
name="search" name="search"

View file

@ -13,6 +13,7 @@
{% endblock %} {% endblock %}
{% block body %} {% block body %}
<h1>{% translate "Transactions" %}</h1> <h1>{% translate "Transactions" %}</h1>
{% if transactions %}
{% transaction_table transactions %} {% transaction_table transactions %}
{% if page_obj %} {% if page_obj %}
<div class="pagination"> <div class="pagination">
@ -29,4 +30,7 @@
</span> </span>
</div> </div>
{% endif %} {% endif %}
{% else %}
<p>{% translate "No transactions to show" %}</p>
{% endif %}
{% endblock %} {% endblock %}

View file

@ -39,6 +39,6 @@ urlpatterns = [
path( path(
"snapshot/<pk>/delete", views.SnapshotDeleteView.as_view(), name="del_snapshot" "snapshot/<pk>/delete", views.SnapshotDeleteView.as_view(), name="del_snapshot"
), ),
path("search", views.search, name="search_post"), path("search", views.SearchView.as_view(), name="search"),
path("search/<search>", views.SearchView.as_view(), name="search"), path("search/<search>", views.SearchView.as_view(), name="search"),
] ]

View file

@ -13,6 +13,7 @@ from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.views.generic import CreateView, DeleteView, ListView, UpdateView from django.views.generic import CreateView, DeleteView, ListView, UpdateView
from django.views.generic.edit import ProcessFormView
from .models import ( from .models import (
Category, Category,
@ -144,18 +145,15 @@ class SnapshotDeleteView(LoginRequiredMixin, DeleteView):
success_url = reverse_lazy("index") success_url = reverse_lazy("index")
@login_required class SearchView(LoginRequiredMixin, ListView, ProcessFormView):
def search(request):
_search = request.POST.get("search")
return redirect("search", search=_search)
class SearchView(LoginRequiredMixin, ListView):
paginate_by = 24 paginate_by = 24
model = Transaction model = Transaction
template_name = "main/transactions.html" template_name = "main/transactions.html"
context_object_name = "transactions" context_object_name = "transactions"
def post(self, *args, **kwargs):
return redirect("search", search=self.request.POST.get("search"))
def get_queryset(self): def get_queryset(self):
self.search = self.kwargs["search"] self.search = self.kwargs["search"]
return ( return (