0

In my django app I have a main page which shows the numbers of books for each genre in a table and it also has a search bar that allows filtering of the books by Genre, title or author which then posts the data to a search page and allows viewing of the book entries in a table.

On the search results page I want to then be able to additionally filter the same set of data further using an extended set of search bars - including the original filters, plus filters such as publish date and rating.

In the extended search I would not want to lose the original search - so for example if I search for horror genre on the main page and then wish to filter that further to only include highly rated books on the sesrch page I dont want to lose the horror filter if I don't add the filter again.

Is this possible? Essentially I'm wondering if you can apply different filters to a single view?

Thank you in advance

3 Answers3

0

I'm not sure if you want the select of genres in the extended filter, or not? Because if yes, I don't see the problem...? If not, why not use a hidden input to pass genre along? forms.CharField(widget=forms.HiddenInput())

BeryCZ
  • 137
  • 4
0

So I guess that your first choice was to do a form for the search and then render the page with the results. It would be nice to see how you pulled this off as we are discussing how to extend it.

Without seeing your implementation of the search feature I think a good example could be to move the search from a form based to a query language.

But let's not get ahead of ourselves! Let's do the easy way for forms after all!

Suppose this is your form

from django import forms
class MyForm(forms):
   title #
   author #
   genre #

Now every time the title, author, genre selections will be sent with a request. Then the easy way out is to just add more stuff to the second form and pass the current state to it when rendering!

from django import forms
class SecondForm(forms):
   title
   author
   genre
   language # new stuff!

So when you get the current data in your function handler/class view you can create a new form SecondForm from the MyForm data, you can read more here.


def refine_search(request):
    # the form has been submitted so it's a safe assumption to have request.method == 'POSt'
    # but this will make it harder to share a link to a search page

    # load the search results
    # prepare the second form to be rendered on the result page
    form = SecondForm(request.POST)
    # now your can render your result page passing the form
    # and it will be rendered with the state from the previous!
edoput
  • 1,212
  • 9
  • 17
  • Thank you edoput. I'm not currently using a form - I essentially take the search criteria from the main page as a get request into my view, then I use the request data to filter a query set and render it to my search page which I want to also have additional search bars allowing further filtering. – Just_Another_day Oct 27 '20 at 22:56
0

django can use many techniques to pass parameters from view to view. e.g. get parameters: https://stackoverflow.com/questions/150505/capturing… or use sessions: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Sessions

Davy
  • 1,720
  • 1
  • 19
  • 42