diff --git a/1.2-requests-templates/recipes/calculator/views.py b/1.2-requests-templates/recipes/calculator/views.py index 0337130fb..9d3a26ced 100644 --- a/1.2-requests-templates/recipes/calculator/views.py +++ b/1.2-requests-templates/recipes/calculator/views.py @@ -19,8 +19,9 @@ # можете добавить свои рецепты ;) } + # Напишите ваш обработчик. Используйте DATA как источник данных -# Результат - render(request, 'calculator/index.html', context) +# Результат - render(request, ) # В качестве контекста должен быть передан словарь с рецептом: # context = { # 'recipe': { @@ -28,3 +29,16 @@ # 'ингредиент2': количество2, # } # } +def dish_calculator(requests, dish_name): + servings = int(requests.GET.get('servings', 1)) + ordered_dish = {} + for dish, ingredients in DATA.items(): + if dish_name == dish: + for ing, amount in ingredients.items(): + ordered_dish[ing] = amount * servings + + context = { + 'recipe': ordered_dish + } + + return render(requests, 'calculator/index.html', context) diff --git a/1.2-requests-templates/recipes/recipes/urls.py b/1.2-requests-templates/recipes/recipes/urls.py index 2d6f1024f..7b37286be 100644 --- a/1.2-requests-templates/recipes/recipes/urls.py +++ b/1.2-requests-templates/recipes/recipes/urls.py @@ -15,7 +15,9 @@ """ from django.urls import path +from calculator.views import dish_calculator urlpatterns = [ + path('/', dish_calculator, name='calculator') # здесь зарегистрируйте вашу view-функцию ]