I found out that I returned the context to my HTML template the view called in the first time, after I hit the submit button the view triggered again from the beginning. how I can prevent the view to triggered from the beginning and execute directly the code after is request.method == 'POST'. this is the code of my view
def recipe_recommendations(request):
print('enter----------------')
# add rating of current user to rating matrix
person = request.session['person_id']
user_category = FoodCategory.objects.filter(
person_id=request.session['person_id']).values_list('category', flat=True)
user_category = user_category[0]
target_user = add_to_csv(person, category=user_category)
# get recommendation
recom_size = 5
htop_n_for_target_user = []
unhtop_n_for_target_user = []
htop_n_for_target_user,unhtop_n_for_target_user = get_top_n_for_user(person,recom_size, user_category)
# get recipe info and send them to tmeplate
id_h_recipes = []
# for i in htop_n_for_target_user:
# id_h_recipes.append(i[0])
[id_h_recipes.append(i[0]) for i in htop_n_for_target_user]
id_unh_recipes = []
[id_unh_recipes.append(i[0]) for i in unhtop_n_for_target_user]
# for i in unhtop_n_for_target_user:
# id_unh_recipes.append(i[0])
print(f'-------healthy_ids: {id_h_recipes}')
print(f'--------unhealthy_ids: {id_unh_recipes}')
# extract 5 healthy recipes
h_0_recipe = HealthyRecipe.objects.get(id=id_h_recipes[0])
h_1_recipe = HealthyRecipe.objects.get(id=id_h_recipes[1])
h_2_recipe = HealthyRecipe.objects.get(id=id_h_recipes[2])
h_3_recipe = HealthyRecipe.objects.get(id=id_h_recipes[3])
h_4_recipe = HealthyRecipe.objects.get(id=id_h_recipes[4])
# extract 5 unhealthy recipes
unh_0_recipe = UnhealthyRecipe.objects.get(id=id_unh_recipes[0])
unh_1_recipe = UnhealthyRecipe.objects.get(id=id_unh_recipes[1])
unh_2_recipe = UnhealthyRecipe.objects.get(id=id_unh_recipes[2])
unh_3_recipe = UnhealthyRecipe.objects.get(id=id_unh_recipes[3])
unh_4_recipe = UnhealthyRecipe.objects.get(id=id_unh_recipes[4])
# selected recipe model
selected_recipe = SelectedRecipe()
if request.method == "POST":
# if the user already select a recipe
person = request.session['person_id']
user_selected = SelectedRecipe.objects.filter(person_id = person)
if user_selected:
SelectedRecipe.objects.filter(person_id=person).delete()
user_selected = Recommendations.objects.filter(person_id = request.session['person_id'])
if user_selected:
Recommendations.objects.filter(person_id=request.session['person_id']).delete()
recipe_name = request.POST.get('recipe_name')
recipe_id = request.POST.get('recipe_id')
recipe_h = request.POST.get('healthiness')
if recipe_h == 'healthy':
nutri__fsa = HealthyRecipe.objects.filter(id=recipe_id).values_list('Nutri_score', 'Fsa_new',)
else:
nutri__fsa = UnhealthyRecipe.objects.filter(id=recipe_id).values_list('Nutri_score', 'Fsa_new')
selected_recipe.Nutri_score = nutri__fsa[0][0]
selected_recipe.fsa_score = nutri__fsa[0][1]
selected_recipe.person_id= person
selected_recipe.recipe_name = recipe_name
selected_recipe.recipe_id = recipe_id
selected_recipe.healthiness = recipe_h
selected_recipe.session_id = request.session['session_id']
selected_recipe.save()
h_recommendations = Recommendations()
h_recommendations.person_id = person
h_recommendations.recommended_recipes = [h_0_recipe.id,h_1_recipe.id,h_2_recipe.id,h_3_recipe.id,h_4_recipe.id]
h_recommendations.healthiness = 'Healthy'
h_recommendations.save()
unh_recommendations = Recommendations()
unh_recommendations.person_id = person
unh_recommendations.recommended_recipes = [unh_0_recipe.id,unh_1_recipe.id,unh_2_recipe.id,unh_3_recipe.id,unh_4_recipe.id]
unh_recommendations.healthiness = 'Unhealthy'
unh_recommendations.save()
return redirect('Labels_Nudges:choice_evaluation')
else:
Serving_size = float(h_0_recipe.size_g) // float(h_0_recipe.Servings)
Serving_size1 = float(h_1_recipe.size_g) // float(h_1_recipe.Servings)
Serving_size2 = float(h_2_recipe.size_g) // float(h_2_recipe.Servings)
Serving_size3 = float(h_3_recipe.size_g) // float(h_3_recipe.Servings)
Serving_size4 = float(h_4_recipe.size_g) // float(h_4_recipe.Servings)
h_0 = [h_0_recipe.Name, id_h_recipes[0], 'healthy', h_0_recipe.image_link, int(float(h_0_recipe.calories_kCal)), int(float(h_0_recipe.Servings)),int(float(Serving_size))]
h_1 = [h_1_recipe.Name, id_h_recipes[1], 'healthy', h_1_recipe.image_link, int(float(h_1_recipe.calories_kCal)), int(float(h_1_recipe.Servings)),int(float(Serving_size1))]
h_2 = [h_2_recipe.Name, id_h_recipes[2], 'healthy', h_2_recipe.image_link, int(float(h_2_recipe.calories_kCal)), int(float(h_2_recipe.Servings)),int(float(Serving_size2))]
h_3 = [h_3_recipe.Name, id_h_recipes[3], 'healthy', h_3_recipe.image_link, int(float(h_3_recipe.calories_kCal)), int(float(h_3_recipe.Servings)),int(float(Serving_size3))]
h_4 = [h_4_recipe.Name, id_h_recipes[4], 'healthy', h_4_recipe.image_link, int(float(h_4_recipe.calories_kCal)), int(float(h_4_recipe.Servings)),int(float(Serving_size4))]
Serving_size_ = float(unh_0_recipe.size_g) // float(unh_0_recipe.Servings)
Serving_size_1 = float(unh_1_recipe.size_g) // float(unh_1_recipe.Servings)
Serving_size_2 = float(unh_2_recipe.size_g) // float(unh_2_recipe.Servings)
Serving_size_3 = float(unh_3_recipe.size_g) // float(unh_3_recipe.Servings)
Serving_size_4 = float(unh_4_recipe.size_g) // float(unh_4_recipe.Servings)
unh_0 = [unh_0_recipe.Name, id_unh_recipes[0], 'unhealthy', unh_0_recipe.image_link, int(float(unh_0_recipe.calories_kCal)), int(float(unh_0_recipe.Servings)),int(float(Serving_size_))]
unh_1 = [unh_1_recipe.Name, id_unh_recipes[1], 'unhealthy', unh_1_recipe.image_link, int(float(unh_1_recipe.calories_kCal)), int(float(unh_1_recipe.Servings)),int(float(Serving_size_1))]
unh_2 = [unh_2_recipe.Name, id_unh_recipes[2], 'unhealthy', unh_2_recipe.image_link, int(float(unh_2_recipe.calories_kCal)), int(float(unh_2_recipe.Servings)),int(float(Serving_size_2))]
unh_3 = [unh_3_recipe.Name, id_unh_recipes[3], 'unhealthy', unh_3_recipe.image_link, int(float(unh_3_recipe.calories_kCal)), int(float(unh_3_recipe.Servings)),int(float(Serving_size_3))]
unh_4 = [unh_4_recipe.Name, id_unh_recipes[4], 'unhealthy', unh_4_recipe.image_link, int(float(unh_4_recipe.calories_kCal)), int(float(unh_4_recipe.Servings)),int(float(Serving_size_4))]
# send forms
return render(request,'Labels_Nudges/recipe_recommendations.html',context={'h_':htop_n_for_target_user,
'unh_':unhtop_n_for_target_user,
'h_0':h_0,
'h_1':h_1,
'h_2':h_2,
'h_3':h_3,
'h_4':h_4,
'unh_0':unh_0,
'unh_1':unh_1,
'unh_2':unh_2,
'unh_3':unh_3,
'unh_4':unh_4,
})```
Aucun commentaire:
Enregistrer un commentaire