Advertisement
12311k

Untitled

Feb 10th, 2021
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. import sys
  2. from django.shortcuts import render
  3. from icecream.models import icecream_db
  4. from anfisa.models import friends_db
  5. from anfisa.services import what_weather
  6.  
  7.  
  8. def index(request):
  9. # если не объявить здесь пустые переменные,
  10. # то при GET-запросе блок if request.method == 'POST' не сработает
  11.  
  12. # В результате, при попытке передать несуществующие переменные в
  13. # словарь context
  14. # программа сломается и сообщит об ошибке
  15. icecreams = ''
  16. friends = ''
  17. city_weather = ''
  18. friend_output = ''
  19.  
  20. for friend in friends_db:
  21. # Около каждого имени вставляется radio button,
  22. # и теперь в форме кликом по кнопочке можно будет
  23. # выбрать одного из друзей.
  24. friends += (f'<input type="radio" name="friend" '
  25. f'required value="{friend}">{friend}<br>')
  26.  
  27. for i in range(len(icecream_db)):
  28. icecreams += (f'{icecream_db[i]["name"]}'
  29. f'<a href="icecream/{i}/">узнать состав</a><br>')
  30.  
  31. # Проверьте, что тип запроса — POST
  32. if request.method == 'POST':
  33. # Извлеките из запроса имя друга
  34. selected_friend = request.POST['friend']
  35. # запишите название города в переменную city
  36. city = friends_db[selected_friend]
  37. # Запросите погоду в городе city
  38. weather = what_weather(city)
  39. # В переменную friend_output запишите строку
  40. # "{Имя_друга}, тебе прислали мороженое!"
  41. friend_output = f'{selected_friend}, тебе прислали мороженое!'
  42. # В переменную city_weather запишите строку
  43. # "Погода в городе [название_города]: [погода_в_городе]"
  44. city_weather = f'Погода в городе {city}: {weather}'
  45.  
  46. # эта строчка выводит переменные в консоль.
  47. # Мы сдеали тестовый запрос с именем Стёпа.
  48. # Жмите ПРОВЕРИТЬ и смотрите в консоле, как появляются данные
  49.  
  50. print(f'имя друга: { selected_friend } \n'
  51. f'город: { city } \n'
  52. f'погода в городе: { weather } \n'
  53. f'текст для вывода: { friend_output } \n'
  54. f'погода для вывода: { city_weather } \n',
  55. file=sys.stderr)
  56.  
  57. context = {
  58. 'icecreams': icecreams,
  59. 'friends': friends,
  60. 'friend_output': friend_output,
  61. 'city_weather': city_weather,
  62. }
  63. return render(request, 'homepage/index.html', context)
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement