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">
{% translate "Snapshot" %}
</a>
<form id="search" action="{% url 'search_post' %}" method="post">
<form id="search" action="{% url 'search' %}" method="post">
{% csrf_token %}
<input type="text"
name="search"

View file

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

View file

@ -39,6 +39,6 @@ urlpatterns = [
path(
"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"),
]

View file

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