I have a search form where the models an user searches are displayed bellow the search form but I want it to be shown in another page. I tried looking for a way to redirect the user to another url when the search is done and display the filtered data there but I wasn't able to do that.
model:
class Product(models.Model):
author = models.ForeignKey(User, default=None, on_delete=models.CASCADE)
title = models.CharField(max_length=120, unique=True)
date_posted = models.DateTimeField(default=timezone.now)
...
view:
def FilterView(request):
qs = Product.objects.all()
title_contains_query = request.GET.get('title_contains')
title_or_author_query = request.GET.get('title_or_author')
...
if is_valid_queryparam(title_contains_query):
qs = qs.filter(title__icontains=title_contains_query)
elif is_valid_queryparam(title_or_author_query):
qs = qs.filter(Q(title__icontains=title_or_author_query) | Q(author__username__icontains=title_or_author_query)).distinct()
...
context = {
'queryset': qs,
...
}
return render(request, 'main/form.html', context)
template:
<form method="GET">
<div>
<input type="search" name="title_contains" placeholder="Title contains">
<span><div><i ></i></div></span>
</div>
<div>
<input type="search" name="title_or_author" placeholder="Title or author">
<span>
<div><i ></i></div>
</span>
</div>
...
<button type="submit">Search</button>
</form>
<div>
<ul>
{% for product in queryset %}
<li>
{{ product.title }}
<span>Author: {{ product.author.name }}</span>
</li>
<hr />
{% endfor %}
</ul>
</div>
CodePudding user response:
You can simply make two views one for displaying form and another for displaying queryset, only need to use action
attribute correctly so:
Note: Assuming urlpatterns by myself(as you haven't shared), hope you'll understand.
urls.py:
urlpatterns=[
path("form/", views.form_display, name="form_display"),
path("some_route/", views.FilterView, name="filter_view")
]
views.py:
def form_display(request):
return render("main/form.html")
def FilterView(request):
qs = Product.objects.all()
title_contains_query = request.GET.get('title_contains')
title_or_author_query = request.GET.get('title_or_author')
...
if is_valid_queryparam(title_contains_query):
qs = qs.filter(title__icontains=title_contains_query)
elif is_valid_queryparam(title_or_author_query):
qs = qs.filter(Q(title__icontains=title_or_author_query) | Q(author__username__icontains=title_or_author_query)).distinct()
...
context = {
'queryset': qs,
...
}
return render(request, 'main/display_query.html', context)
main/form.html:
<form method="GET" action="{% url 'filter_view' %}">
<div>
<input type="search" name="title_contains" placeholder="Title contains">
<span><div><i ></i></div></span>
</div>
<div>
<input type="search" name="title_or_author" placeholder="Title or author">
<span> <div><i ></i></div> </span>
</div>
...
<button type="submit">Search</button>
</form>
main/display_query.html:
<div>
<ul>
{% for product in queryset %}
<li>
{{ product.title }}
<span>Author: {{ product.author.name }}</span>
</li>
<hr/>
{% endfor %}
</ul>
</div>
Note: Function based views are written in
snake_case
notPascalCase
unlike class based, so you can change it tofilter_view
fromFilterView
.