1

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
4
  • The code in AgencyFullView seems to be written right in the body of the class. Is that really the case? Also, there seems to be no agency_count variable in the mixin class either, you have agency_count_test. Mind you that agency_count_test is a local variable and it will go away once the get_context_data() method returns. Commented Sep 6, 2017 at 20:37
  • You are trying to set your template at the class level based on something that will only be calculated on request. This is not going to work. Your if agency_count != 0 code will only run once at the application start, not for every request and so cannot use this count. Commented Sep 6, 2017 at 20:42
  • @Mad Wombat I have removed the _test typo Commented Sep 6, 2017 at 21:46
  • Please post your solution as an answer, instead of adding it to your question. The code you have posted doesn't make sense -- you shouldn't have return outside of a method. Commented Sep 7, 2017 at 15:40

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

I get the following error: 'super' object has no attribute 'get'. This line is throwing the error: return super(AgencyFullMixin, self).get(request, *args, **kwargs)
Try changing the order of your classes.
1

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.