Fish
Fish

Reputation: 225

How to access a variable in inherited class based views

I am wondering how to run a reality check to decide which template file to use. How do I access agency_count from AgencyFullView? What I currently have returns type object 'AgencyFullMixin' has no attribute 'agency_count'

class AgencyFullMixin(ContextMixin):

    def get_context_data(self, pk, **kwargs):
        context_data = super(AgencyFullMixin, self).get_context_data(**kwargs)
        agency = Agencies.objects.filter(pk=pk)
        context_data["agency"] = agency
        agency_count = agency.count()
        context_data["agency_count"] = agency_count
        return context_data

class AgencyFullView(TemplateView, AgencyFullMixin):

    if agency_count != 0:    **<<<--- What to put here?**
        template_name = 'community_information_database/agency_full.html'
    else: 
        template_name = 'community_information_database/not_valid.html'

    def get_context_data(self, **kwargs):
        context_data = super(AgencyFullView, self).get_context_data(**kwargs)
        return context_data

Upvotes: 1

Views: 844

Answers (2)

Fish
Fish

Reputation: 225


Fixed: Here's the solution I am using:

class AgencyFullMixin(ContextMixin):

    def get_context_data(self, pk, **kwargs):
        context_data = super(AgencyFullMixin, self).get_context_data(**kwargs)
        agency = Agencies.objects.filter(pk=pk)
        context_data["agency"] = agency
        agency_count = agency.count()
        context_data["agency_count"] = agency_count
        return context_data

class AgencyFullView(TemplateView, AgencyFullMixin):
    def get_template_names(self, **kwargs):
        agency = Agencies.objects.filter(pk=self.kwargs['pk']).filter(pk__isnull=False)
        if agency:
            return 'community_information_database/agency_full.html'
        else:
            return 'community_information_database/not_valid.html'

    def get_context_data(self, **kwargs):
        context_data = super(AgencyFullView, self).get_context_data(**kwargs)
        return context_data

Upvotes: 1

Alasdair
Alasdair

Reputation: 308849

If you want to access agency_count in another method, then you'll have to set it as an attribute. You could do this in the dispatch method.

class AgencyFullMixin(ContextMixin):
    def dispatch(self, request, *args, **kwargs):
        agencies = Agencies.objects.filter(pk=self.kwargs['pk'])
        self.agency_count = agencies.count()
        return super(AgencyFullMixin, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        """
        Add the agency count to the context
        """
        context = super(AgencyFullMixin, self).get_context_data(**kwargs)
        context['agency_count'] = self.agency_count
        return context

You can then access self.agency_count in other methods. To change the template name dynamically, you should override get_template_names.

class AgencyFullView(AgencyFullMixin, TemplateView):
    def get_template_names(self):
        if self.agency_count != 0:
            template = 'community_information_database/agency_full.html'
        else: 
            template = 'community_information_database/not_valid.html'
        return [template]  # nb get_template_names must return a list

Upvotes: 3

Related Questions