I know there is a Django shop you can implement but it doesn't seem to fit my need. I have a python code which returns a 2d list, which I then use jinja to display in HTML. Each list in the 2d list contains information about products and I was wondering if its possible to add an add to basket feature which stores that list to the user, I was thinking in a form of a dictionary, so the user can later see their basket.
Asked
Active
Viewed 104 times
1 Answers
0
You have several options for this, depending what you want for your Database layout. You could extend the django user model with a nwe field that holds a dict. A more elegant (and probably better) solution is to create a new DB Table which holds the entries of your basket and the User ID. For this there are several designs, two of which are: -Two Tables, one holds all items (columns item and id), one connects item id to user id -One table with three entries, id, item and user id You get the IDs here by adding the column as foreign key
Choose an option to your liking and dependant on your level of education regaring databases. And probably draw your layout out on paper first before implementing, so you can see if it makes sense.

Ueda Ichitaka
- 71
- 1
- 8
-
HI, I am doing the first option as I have limited experience and i have made a dictionary called cart in models.py. I have a button which i want to just add to the dictionary when clicked. The issue i'm having is I havn't found a way to add to it with jinja. so my question is what is the best way to do add to the dictionary? – Deps Feb 26 '18 at 14:58
-
Create a simple html button and link it with a view function like discribed here: https://stackoverflow.com/questions/15341285/how-to-call-a-django-function-on-button-click In your View function just receive the id of the item you want to add and the user id, filter through the db tables, add and save. Vóila. – Ueda Ichitaka Feb 26 '18 at 19:02
-
Ive put it into a form with the action linking to a url, which then calls a function, but I cant find a way to get the list from the html file into the function – Deps Mar 01 '18 at 11:49
-
Why back from the html? You hold the list within your view function and just display it on the html. when you add an item, you add it to the list and reload the display of the html file. – Ueda Ichitaka Mar 12 '18 at 10:30
-
Thanks. I ended up storing all the results the user searched for in the session and once then they clicked the add to basket button it adds the product, which it gets from the session, to the database. This might not be the most efficient way but it works for me. Thanks! – Deps Mar 12 '18 at 18:26