Reputation: 167
I am trying to create a search button for my database. But my self.request.POST.get('searched') is returning None
the form:
<form class="d-flex" action="{% url 'asset_app_search' %}">{% csrf_token %}
<input class="form-control me-2" type="search" placeholder="Søg" aria-label="Search" name="searched">
<button class="btn btn-outline-secondary" type="submit">Søg</button> -
</form>
my views.py
class SearchView(generic.TemplateView):
template_name = "asset_app/search.html"
def post(self):
searched = self.request.POST.get('searched')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
searched = self.post()
context['searched'] = searched
context_entry_today = datetime.date.today()
context_entry_overdue = datetime.date.today() - datetime.timedelta(days=90)
context_entry_inspection_time = datetime.date.today() - datetime.timedelta(days=76)
context['assets'] = models.Asset.objects.order_by('name')
context['rooms'] = models.Room.objects.order_by('last_inspected', 'location', 'name')
context['bundelReservations'] = models.Bundle_reservation.objects.order_by('return_date')
context['loan_assets'] = models.Loan_asset.objects.order_by('return_date')
context['to_dos'] = to_do_list_app.models.Jobs.objects.all()
context['today'] = context_entry_today
context['overdue'] = context_entry_overdue
context['inspection_time'] = context_entry_inspection_time
return context
and what is beeing posted
[11/Jun/2021 22:55:23] "GET /asset/search/?csrfmiddlewaretoken=fqb8jppygSbZ10ET8AXw6dd5B77z5OYudNJU0uyjp8jFNYDG57nkNvrcx5lHFsPo&searched=sdfdsff HTTP/1.1" 200 10418
Upvotes: 1
Views: 1349
Reputation: 476699
You should let the form make a POST request, with:
<form method="post" class="d-flex" action="{% url 'asset_app_search' %}">{% csrf_token %}
…
</form>
You thus specify method="post"
in the <form>
tag.
In your view, your post
method will eventually have to return a HttpResponse
object:
class SearchView(generic.TemplateView):
template_name = "asset_app/search.html"
def post(self):
searched = self.request.POST.get('searched')
# …
return HttpResponse('some message')
Upvotes: 1