Untitled
3 years ago in Plain Text
from django.shortcuts import render
from icecream.models import icecream_db
from anfisa.models import friends_db
from anfisa.services import what_weather
def index(request):
icecreams = ''
friends = ''
city_weather = ''
friend_output = ''
# добавили выбранное мороженное, которое получим из формы
selected_icecream = ''
for friend in friends_db:
# Около каждого имени вставляется radio button,
# и теперь в форме кликом по кнопочке можно будет выбрать одного из друзей.
friends += (f'<input type="radio" name="friend"'
f' required value="{friend}">{friend}<br>')
# из списка ссылок icecreams сделаем список чекбоксов
for i in range(len(icecream_db)):
ice_form = (f'<input type="radio" name="icecream" required'
f' value="{icecream_db[i]["name"]}">{icecream_db[i]["name"]}')
ice_link = f'<a href="icecream/{i}/">узнать состав</a>'
icecreams += f'{ice_form} | {ice_link} <br>'
if request.method == 'POST':
# Извлекли из запроса имя друга
selected_friend = request.POST['friend']
# В переменную selected_icecream запишите
# название мороженого из POST-запроса
selected_icecream = request.POST['icecream']
# записали название города в переменную city
city = friends_db[selected_friend]
# Запросили погоду в городе city
weather = what_weather(city)
# Вместо слова "мороженое" выведите название сорта из запроса.
friend_output = f'{selected_friend}, тебе прислали {selected_icecream}!'
city_weather = f'В городе {city} погода: {weather}'
context = {
'icecreams': icecreams,
'friends': friends,
'friend_output': friend_output,
'city_weather': city_weather,
}
return render(request, 'homepage/index.html', context)