I’m learning Python, Django, Javascript and jQuery by trying to build a simple shopping list web application. I’d like to hear from experienced developers if my approach is correct:
Functionality:
Web page will display a list of foods. When I click on a certain food I want a list of ingredients to get displayed (this will be my shopping list). Clicking another food will add more ingredients to the shopping list.
My approach:
I have built a simple page with django. I’m able to display a list of all foods that have been entered to the database. What I need now is to start building a list of ingredients as I click on food items.
I was thinking I would load a list of ingredients from the database on the first page load but hide them with css. When I click on a food item I would un-hide the ingredients associated with the food item by using jquery/css.
This approach seems quite clumsy to me. Could you give me some advice how I could create my shopping list application using the technologies mentioned above? Is my approach correct?
Here’s how I’d do it. But note that there will be no right answer to this question.
Lets call the page where you click on foods,
index.php.On
index, I’d have all the foods that could be clicked, as well as an empty div with the id “ingredients” — eg.<div id="ingredients"></div>When foods were clicked, they’d call jQuery handlers in
main.js.The onClick handlers would:
get_ingredients.phppage via AJAX.More explicitly,
get_ingredients.phpwould:indexecho/print/printf/etc, this isn’t really displayed with AJAX, but rather sent as the AJAX response.This way, you only have to keep track of the selected foods, and not deal with adding/subtracting individual ingredient quantities when foods are selected and deselected.
This has the downside of re-transmitting things “you already know”, namely the ingredients of foods that were previously selected, however it eliminates a lot of the work that would otherwise be required.