Charles Smith
Charles Smith

Reputation: 3289

Writing Conditional Django Views

I have been racking my brain all day to get past a few challenges in my project. I have a view that is currently a ListView, but I need to display conditional logic as well and I am too much of a noob with Django, I'm not sure of the questions to ask.

Displaying the list in my template is the easy part, but if you look at the code below, I also have a centext total_sales defined to aggregate the sum of the 'sales' column - this also works. What I need is to be able to display the Sum if the 'day_of_week' field == "Monday" or any other day of week in the if statement.

UPDATE: Sorry, I realized my question didn't include enough information. 'day_of_week' is a field in the Traffic model and I would like to show the Sum of the 'sales' field, but only if 'day_of_week' is equal to a specific day. I am new to Django and although I know how to write if statements, I am completely unsure how to place them in my view in this instance so I can render the totals in my template.

Is there a way to write conditional data in the view how I currently have it setup? Thank you for your time.

class TrafficListView(ListView):
    model = Traffic
    template_name = 'dashboard/pages/traffic.html'

    def get_context_data(self, **kwargs):
        context = super(TrafficListView, self).get_context_data(**kwargs)
        context['total_sales'] = Traffic.objects.all().aggregate(Sum('sales')).get('sales__sum', 0.00)
        return context

Upvotes: 0

Views: 1643

Answers (1)

Rahul Gupta
Rahul Gupta

Reputation: 47876

Since you need to display the total sales for each day of the week and not a particular day as you mentioned in the comments above, you can do the following:

class TrafficListView(ListView):
    model = Traffic
    template_name = 'dashboard/pages/traffic.html'

    def get_context_data(self, **kwargs):
        context = super(TrafficListView, self).get_context_data(**kwargs)
        days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday'] # days of the week
        total_sales = {} # create an empty dictionary 
        for day in days:
            # get the sales for that particular day 
            total_sales[day] = Traffic.objects.filter(day_of_week=day).aggregate(Sum('sales')).get('sales__sum', 0.00)
        context['total_sales'] = total_sales # pass 'total_sales' dictionary in context
        return context

Here, we create a list days having values from Monday to Sunday. Then we create an empty dictionary total_sales.

We will now iterate through the days list and filter all the objects from Traffic model where day_of_week is equal to that particular day. lets say Monday. Then we perform Sum aggregation on the objects returned for day_of_week as Monday on the sales field. The value returned from sales_sum is stored in a key Monday in the total_sales dictionary. So, after iteration is complete, there will be 7 keys in the total_sales for each day of the week with the value being the sum of sales for that particular day.

In your template, use {{total_sales.Monday}} to access Monday sales, {{total_sales.Tuesday}} for Tuesday sales and so on.

Upvotes: 1

Related Questions