On my homepage I have a search bar and when you search something it redirects you to a page with the results(titles and document types). On the left side of the page I want to implement a filter by document type.
After the search my url looks like this: http://127.0.0.1:8000/search/?q=something
After applying the filter: http://127.0.0.1:8000/search/?document_type=Tehnical+report
I don't know how to implement the filters to search just in the objects list filtered by the query (q) on the search page. Also, I'm not sure if the url should look like this : http://127.0.0.1:8000/search/?q=something&document_type=Tehnical+report or like this http://127.0.0.1:8000/search/?document_type=Tehnical+report after applying the filter.
models.py
DOCUMENT_TYPES = [
('Tehnical report','Tehnical report'),
('Bachelor thesis','Bachelor thesis'),
...
]
class Form_Data(models.Model):
title = models.CharField(unique=True, max_length=100, blank=False)
author = models.CharField(max_length=100)
document_type = models.CharField(choices=DOCUMENT_TYPES, max_length=255, blank=False, default=None)
views.py
def search_list(request):
object_list = Form_Data.objects.none()
document_types = DOCUMENT_TYPES
query = request.GET.get('q')
query_list = re.split("\s|(?<!\d)[,.](?!\d)", query)
document_type_query = request.GET.get('document_type')
for item in query_list:
object_list |= Form_Data.objects.filter( Q(title__icontains=item) | Q(author__icontains=item))
return render(request, "Home_Page/search_results.html")
home_page.html
<div class="Search">
<form action="{% url 'home_page:search_results' %}" method="get">
<input id="Search_Bar" type="text" name="q">
<button id="Button_Search" type="submit"></button>
</form>
</div>
search_results.html
{% for form_data in object_list %}
<h5>{{ form_data.title }}</h5>
<h5>{{ form_data.document_type }}</h5>
{% endfor %}
<form method="GET" action=".">
<select class="form-control" name="document_type">
{% for tag, label in document_types %}
<option value="{{ tag }}">{{ tag }}</option>
{% endfor %}
</select>
</form>