user3418337 user3418337 - 4 months ago 26
Python Question

Django: Filter, list, session

I have some question on Django and how to use it to solve the problem below.
Suppose you have this two table
Products table

---------------------------------------------------------------
| id | productName | description | id_country |
---------------------------------------------------------------
| 1 | x | fzefzzezfz | 1 |
---------------------------------------------------------------
| 2 | y | zoinojnfze | 1 |
---------------------------------------------------------------
| 3 | az | ononbonoj | 2 |
---------------------------------------------------------------


country table

-----------------------
| id | name |
-----------------------
| 1 | france |
-----------------------
| 2 | spain |
-----------------------


and these urls:

http://www.exemple.com/list/ (list all products)
http://www.exemple.com/add/ (add a new product)
http://www.exemple.com/detail/1 (print details about product with id=1)


What I want to do is to allow visitors of website to set a filter for the duration of their navigation, so for every display of the product list
only product from France or Spain will be displayed depending on the filter.

I could use french.exemple.com or spain.exemple.com to filter result but i dont want to ducplicate the code for every
subdomain.

How would handle this problem?

Answer

You've said it yourself in the question tags: use the session.

When the user chooses a country, set that value in the request.session dict; then, in each of your views, filter the products by that value.