3
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

One option is to modify your TemplateView class to include the peak's name as an instance attribute (say peak_name) and make the get_context_data method make use of it:

class  TemplateView():
    # Class attributes

    def __init__(self, peak_name):
        # Instance attributes
        # ...
        self.peak_name = peak_name

    # Class methods

    # Modification of the get_context_data method
    def get_context_data(self, **kwargs):
        context['Peak'] = PeakForecast.objects.filter(
            name_of_peak=self.peak_name).order_by('date')  #<- Modification here
        print(context['Peak'])
        return context

And then you can generate your peak views as instances of that class:

krivan_view = TemplateView('Krivan')
krivan_view.get_context_data(arg1='arg1', arg2='arg2')
\$\endgroup\$
2
  • \$\begingroup\$ Thanks that was really helpful. And I just made a class which inherit from TemplateView and in that class I made modification and then rest classes inherit from that one. \$\endgroup\$ Commented Aug 20, 2020 at 17:10
  • \$\begingroup\$ You're welcome. I'm glad this was helpful. \$\endgroup\$ Commented Aug 20, 2020 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.