def get(self, request, departure): if departure not in departures: return HttpResponseNotFound('Ой, данная страница не найдена((') departure_tours = {} tours_price = [] tours_nights = [] max_stars = "★★★★★" for tour_id, tour in tours.items(): if departure in tour.values(): departure_tours[tour_id] = tours[tour_id] tours_price.append(tour["price"]) tours_nights.append(tour["nights"]) context = { "departure": departures[departure], "tours": departure_tours, "stars": max_stars, "min_price": min(tours_price), "max_price": max(tours_price), "min_nights": min(tours_nights), "max_nights": max(tours_nights) } return render(request, 'departure.html', context=context)
def get(self, request, departure: str): if departure not in departures.keys(): raise Http404('Страница не найдена') departure_tours = dict(((tour_id, tour) for (tour_id, tour) in tours.items() if tour['departure'] == departure)) tours_price = list( (tour['price']) for tour in departure_tours.values()) tours_nights = list( (tour['nights']) for tour in departure_tours.values()) price_min = min(tours_price) price_max = max(tours_price) nights_max = max(tours_nights) nights_min = min(tours_nights) tourcount = len(departure_tours) return render( request, 'departure.html', { 'title': title, 'departure_tours': departure_tours, 'departure': departures[departure], 'departures': departures, 'price_min': price_min, 'price_max': price_max, 'nights_min': nights_min, 'nights_max': nights_max, 'tourcount': tourcount, })
def get(self, request, departure): select_departure = {} list_id = [] list_amount_nights = [] list_prices = [] for tour_id, tour in tours.items(): if tour["departure"] == departure: select_departure[tour_id] = tour list_id.append(tour_id) list_prices.append(tours[tour_id]["price"]) list_amount_nights.append(tours[tour_id]['nights']) if departure not in departures.keys(): raise Http404 min_price = min(list_prices) max_price = max(list_prices) min_nights = min(list_amount_nights) max_nights = max(list_amount_nights) context = { 'select_departure': select_departure, 'departure': departures[departure], 'amount_tours': len(list_id), 'min_price': min_price, 'max_price': max_price, 'min_nights': min_nights, 'max_nights': max_nights, 'title': title } return render(request, 'departure.html', context=context)
def get(self, request, departure): if departure not in departures: return HttpResponse('Данного направления нет в базе') departure_tours = {} prices = [] nights = [] for idx, tour in tours.items(): if tour['departure'] == departure: departure_tours[idx] = tour prices.append(int(tour['price'])) nights.append(int(tour['nights'])) min_max_prices = {'min': min(prices), 'max': max(prices)} min_max_nights = {'min': min(nights), 'max': max(nights)} return render(request, 'tours/departure.html', context={ 'departure': departures.get(departure), 'tours': departure_tours, 'min_max_prices': min_max_prices, 'min_max_nights': min_max_nights, })
def get(self, request, departure_id): tour_copy = {} min_price = 50000000 max_price = 0 min_days = 100000 max_days = 0 count = 0 for key, value in tours.items(): if value['departure'] == departure_id: tour_copy[key] = tours[key] count += 1 min_days = min(min_days, tours[key]['nights']) max_days = max(max_days, tours[key]['nights']) min_price = min(min_price, tours[key]['price']) max_price = max(max_price, tours[key]['price']) t_t = 'тур' if (20 >= count >= 10) or ((count % 10) in [0, 5, 6, 7, 8, 9]): t_t += 'ов' elif (count % 10) in [2, 3, 4]: t_t += 'а' return render( request, self.template_name, { "title": title, "departures": departures, "current": departures[departure_id], "tours": tour_copy, "count": count, "min_price": min_price, "max_price": max_price, 't_t': t_t, "min_days": min_days, "max_days": max_days })
def main_view(request): rand_tours = dict(random.sample(tours.items(), 6)) return render( request, 'tours/index.html', { 'title': title, 'description': description, 'subtitle': subtitle, 'tours': rand_tours, })
def main_view(request): tours_rand = random.sample(tours.items(), 6) return render( request, 'tours/index.html', { 'title': title, 'subtitle': subtitle, 'description': description, 'tours_rand': tours_rand })
def main_view(request): random_tours = dict(random.sample(tours.items(), 6)) context = { "header": data.title, "subtitle": data.subtitle, "description": data.description, "departures": data.departures, "tours": random_tours } return render(request, 'tours/index.html', context=context)
def departure_view(request, dep_id): dep_tours = {} for k, v in tours.items(): if v['departure'] == dep_id: dep_tours[k] = v return render(request, 'departure.html', { 'tours': dep_tours, 'departures': departures, 'from': departures[dep_id] })
def main_view(request): random_tours = dict(random.sample(tours.items(), 6)) return render(request, "tours/index.html", context={ 'title': title, 'subtitle': subtitle, 'description': description, 'tours': random_tours, 'departures': departures, })
def get(self, request, *args, **kwargs): random_six_tours = random.sample(tours.items(), 6) return render(request, 'tours/index.html', context={ 'site_title': title, 'site_subtitle': subtitle, 'site_description': description, 'menu': departures, 'tours': random_six_tours, })
def get(self, request): index_tours = dict(((tour_id, tour) for (tour_id, tour) in tours.items() if tour_id <= 6)) return render( request, 'index.html', { 'index_tours': index_tours, 'title': title, 'subtitle': subtitle, 'description': description, 'departures': departures, })
def get(self, request, id, *args, **kwargs): city = "" for k, v in tour.items(): if id == k: for k1, v1 in v.items(): if k1 == "departure": for key, value in dep.items(): if key == v1: city = value if id not in tour.keys(): raise Http404 context = {'tours': tour[id], 'city': city} return render(request, 'tours/tour.html', context=context)
def departure_view(request, departure): departure_name = data.departures[departure].replace("Из", "из") tours_list = {} for k, v in tours.items(): if v['departure'] == departure: tours_list.update({k: v}) prices = [price['price'] for price in tours_list.values()] nights = [night['nights'] for night in tours_list.values()] context = { "departures": data.departures, "departure": departure_name, "tours": tours_list, "pricemin": min(prices), "pricemax": max(prices), "nightmin": min(nights), "nightmax": max(nights), } return render(request, 'tours/departure.html', context=context)
def tours_info(departure): search_tours_info = { 'tours_count': 0, 'max_price': 0, 'min_price': 0, 'max_nights': 0, 'min_nights': 0 } tours_price, tours_nights = [], [] for dep_id, dep_value in tours.items(): if dep_value['departure'] == departure: search_tours_info['tours_count'] += 1 tours_price.append(dep_value['price']) tours_nights.append(dep_value['nights']) search_tours_info['max_price'] = max(tours_price) search_tours_info['min_price'] = min(tours_price) search_tours_info['max_nights'] = max(tours_nights) search_tours_info['min_nights'] = min(tours_nights) return search_tours_info
def departure_view(request, departure): context = {'departure': departures[departure]} prices = [] nights = [] needed_tours = [] paths = [] for tour_id, tour in tours.items(): if tour['departure'] == departure: prices.append(tour['price']) nights.append(tour['nights']) needed_tours.append([tour, tour_id]) paths.append(tour_id) context['min_price'] = min(prices) context['max_price'] = max(prices) context['min_nights'] = min(nights) context['max_nights'] = max(nights) context['tours'] = needed_tours context['paths'] = paths return render(request, "tours/departure.html", context=context)
def tour_view(request, id_): # tours = data.tours # departures = data.departures for key, val01 in tours.items(): if key == int(id_): for ke, va in val01.items(): if ke == "title": title = va if ke == "stars": stars = "★" * int(va) if ke == "country": country = va if ke == "price": price = va if ke == "picture": picture = va if ke == "nights": nights = va if ke == "description": description = va if ke == "departure": departure2 = va departure1 = departures[va] return render(request, "tours/tour.html", context={ 'title': title, 'tours': tours, 'stars': stars, 'price': price, 'country': country, 'picture': picture, 'nights': nights, 'departure2': departure2, 'departure1': departure1, 'description': description, 'departures': departures })
def get(self, request, departure, *args, **kwargs): kol_tours = 0 # не хотел этот кусок кода писать здесь, знаю не правильно dict = {} # словарь, для туров, из конкретного города mas = [] max_price = 0 min_price = 100000 max_nights = 0 min_nights = 1000 for key, value in tour.items(): for key1, value1 in value.items(): if departure == value1: kol_tours += 1 dict.update({key: value}) # формирования словаря for key, value in dict.items(): for key1, value1 in value.items(): if key1 == 'price': if value1 > max_price: max_price = value1 if value1 < min_price: min_price = value1 if key1 == 'nights': if value1 > max_nights: max_nights = value1 if value1 < min_nights: min_nights = value1 for k, v in dict.items(): v.update({'KEY': k}) # добовляем номер нашего тура в словарь mas.append(v) # делаем из словаря - список, для удобства context = { 'departures': dep[departure], 'kol_tours': kol_tours, 'max_price': max_price, 'min_price': min_price, 'max_nights': max_nights, 'min_nights': min_nights, 'mas': mas } return render(request, 'tours/departure.html', context=context)
def get(self, request, departure): tours_with_departure = { tour_id: tour_item for tour_id, tour_item in tours.items() if tour_item['departure'] == departure } context = { 'tours': tours_with_departure, 'fly_from': departures[departure], 'total_count_tours': len(tours_with_departure), 'min_price': to_pretty_price(get_min_value(tours_with_departure, 'price')), 'max_price': to_pretty_price(get_max_value(tours_with_departure, 'price')), 'min_nights': get_min_value(tours_with_departure, 'nights'), 'max_nights': get_max_value(tours_with_departure, 'nights'), } return render(request, 'tours/departure.html', context)
def get(self, request, departure): if departure not in departures.keys(): raise Http404 departure_tours = {key: value for key, value in tours.items() if value['departure'] == departure} list_tour = departure_tours.values() max_cost = max(list_tour, key=lambda x: x['price']) min_cost = min(list_tour, key=lambda x: x['price']) max_nights = max(list_tour, key=lambda x: x['nights']) min_nights = min(list_tour, key=lambda x: x['nights']) context = {'site_title': title, 'tour_title': "", 'active': departure, 'departure': departures[departure], 'departure_tours': departure_tours, 'tour_count': len(departure_tours), 'max_cost': max_cost['price'], 'max_nights': max_nights['nights'], 'min_cost': min_cost['price'], 'min_nights': min_nights['nights'], 'departures': departures, } print(context['departure'], context['max_cost'], context['min_cost']) return render(request, 'departure.html', context=context)
def get(self, request, departure, *args, **kwargs): if departure in departures: specific_tour = dict() prices = list() nights = list() for k, v in tours.items(): if tours[k]['departure'] == departure: specific_tour[k] = tours[k] prices.append(tours[k]['price']) nights.append(tours[k]['nights']) return render(request, 'tours/departure.html', context={ 'site_title': title, 'menu': departures, 'departure_name': departures[departure], 'specific_tour': specific_tour, 'min_nights': min(nights), 'max_nights': max(nights), 'min_price': min(prices), 'max_price': max(prices), }) else: raise Http404
def departure_ture(departure): tours_dict = {} for tour_id, tour_value in tours.items(): if tour_value['departure'] == departure: tours_dict[tour_id] = tour_value return tours_dict
# Create your views here. #from django.conf import settings from django.shortcuts import render from django.views import View # импорт данных из tours/data.py from tours.data import (title, subtitle, description, departures, tours) # импорт модуля для генерации случайных чисел import random # Дополнение в словари ключа id соответствуюего номеру тура # можно (и стоило бы) добавить их непосредственно в данные, но я # решил немного вспомнить работу со словарями for key, value in tours.items(): value['id'] = key #обработчики class MainView(View): def get(self, request): #генерация списка 6 случайных неповторяющихся туров из полного словаря tours rand_tours = random.sample([tour for tour in tours.values()], 6) return render(request, 'index.html', context={ 'title': title, 'subtitle': subtitle, 'description': description, 'departures': departures, 'tours': rand_tours
def depart_filter(departure): depart_name_dict = {} for dep_id, dep_value in tours.items(): if dep_value['departure'] == departure: depart_name_dict[dep_id] = dep_value return depart_name_dict