0

I have a view that will show some products. Also I have a filter on the left side.

For example:

Price: Less than $5, $5 - $10 ... etc. Brand: Apple, IBM, Intel etc.

the problem with the links now.

 <a href="?brand=IBM">IBM</a>

 <a href="?price=0|5">From 0 to $5</a>

Now I need these queries to be combined together. However, each link will remove the other queries.

Expected Result:

  ?brand=IBM&price=0|5

I know I can do it using many if else. But is there anyway to do it in django or HTML to make this happen?

Another Explanation:

I need a way that will keep adding to the query in the URL. like amazon when you filter the products. First you pick the brand then you pick the price range then etc. I don't want to check every time if there is a query in the Url or not. I just want a way that will not remove the current query and just keep adding to it.

Othman
  • 2,942
  • 3
  • 22
  • 31
  • Which link would you click in order to get both? Or does clicking a link add onto the URL? –  Nov 19 '14 at 22:03
  • Is there any reason you don't want to do this by passing parameters to your urls? – ekrah Nov 19 '14 at 22:03
  • Look at the [`urllib.urlparse`](https://docs.python.org/3/library/urllib.parse.html#module-urllib.parse) module (just [`urlparse`](https://docs.python.org/2/library/urlparse.html) in Python 2.x). It shows you how to parse a query string. Then you probably want to use some HTML library like `BeautifulSoup` to build a new `a` node for the combined result. – abarnert Nov 19 '14 at 22:03
  • @Xero I need a way that will keep adding to the query in the URL. like amazon when filter the products. First you pick the brand then you pick the price range then etc. I don't want to check everytime if there is a query in the Url or not. I just want a way that will not remove the current query and just keep adding to it. – Othman Nov 19 '14 at 22:07

1 Answers1

0

The following template tag should solve your problem: https://stackoverflow.com/a/24135527/176186.

You might also think about using a proven app like django-filter.

Community
  • 1
  • 1
Tomasz Zieliński
  • 16,136
  • 7
  • 59
  • 83