Self hosted FLOSS fitness/workout, nutrition and weight tracker
Pass the ingredient ID back to the template.
Roland Geiderdc9e6ea1b3aa
diff --git a/nutrition/templates/edit_meal_item.html b/nutrition/templates/edit_meal_item.htmlindex 1ecb140e0..c8d11412b 100644--- a/nutrition/templates/edit_meal_item.html+++ b/nutrition/templates/edit_meal_item.html@@ -32,7 +32,7 @@ $(document).ready(function() {{% include "form_snippet.html" with field=form.amount_gramm %}{% include "form_snippet.html" with field=form.amount_freetext %}{% if js_active %}- <input type="hidden" id="id_ingredient" name="ingredient">+ <input type="hidden" id="id_ingredient" name="ingredient" value="{{ingredient}}">{% include "form_snippet.html" with field=form.ingredient_freetext %}{% else %}{% include "form_snippet.html" with field=form.ingredient css_class="ym-fbox-select" %}diff --git a/nutrition/views.py b/nutrition/views.pyindex 3e9483149..57b2fbd5b 100644--- a/nutrition/views.py+++ b/nutrition/views.py@@ -361,6 +361,8 @@ def edit_meal_item(request, id, meal_id, item_id=None):meal_item = MealItem()else:meal_item = get_object_or_404(MealItem, pk=item_id)+ template_data['ingredient'] = meal_item.ingredient.id+template_data['meal_item'] = meal_item@@ -375,6 +377,12 @@ def edit_meal_item(request, id, meal_id, item_id=None):if request.method == 'POST':meal_form = MealItemForm(request.POST, instance=meal_item)+ # Pass the ingredient ID back to the template, this is originally set by the JQuery+ # autocompleter, in case of errors (max. gramms reached), we need to set the ID manually+ # so the user can simply submit again.+ template_data['ingredient'] = request.POST.get('ingredient', '')++# If the data is valid, save and redirectif meal_form.is_valid():meal_item = meal_form.save(commit=False)