I have built a website in Django in which I tried for the first time to create a complex app. It's an app that scrapes weather conditions on a peak in mountains, which are 17. I wanted to show a detailed forecast in separate templates, so I have 17 views which look almost the same.
Only 4 views:
class KasprowyWierchForecastView(TemplateView):
"""
class for rendering view of detailed weather forecast for
Kasprowy Wierch peak
"""
template_name = 'mountain_base.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['Peak'] = PeakForecast.objects.filter(
name_of_peak='Kasprowy').order_by('date')
print(context['Peak'])
return context
class KoscielecForecastView(TemplateView):
"""
class for rendering view of detailed weather forecast for
Koscielec peak
"""
template_name = 'mountain_base.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['Peak'] = PeakForecast.objects.filter(
name_of_peak='Koscielec').order_by('date')
return context
class KrivanForecastView(TemplateView):
"""
class for rendering view of detailed weather forecast for Krivan peak
"""
template_name = 'mountain_base.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['Peak'] = PeakForecast.objects.filter(
name_of_peak='Krivan').order_by('date')
return context
class MieguszowieckiForecastView(TemplateView):
"""
class for rendering view of detailed weather forecast for
Mieguszowiecki peak
"""
template_name = 'mountain_base.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['Peak'] = PeakForecast.objects.filter(
name_of_peak='Mieguszowiecki').order_by('date')
return context
class MnichForecastView(TemplateView):
"""
class for rendering view of detailed weather forecast for
Mnich peak
"""
template_name = 'mountain_base.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['Peak'] = PeakForecast.objects.filter(
name_of_peak='Mnich').order_by('date')
return context
Only the name of the view class and context['Peak']
are different. The rest of the code is redundant. As my experience is based only on my self learning process I've not found any nice solution on how to fix or refactor.