Calculating income and expense by filtering according to the entered date

Issue

I have a date picker and I will calculate income and expense based on this date. I want to add a parameter named random_date to my function in view.py. I want the user to set the end date. The start time will be today’s date. For example, if the user has selected June 1 as the date, I want to sum the income and expenses from today to June 1st. How can I do that?

Here is my views.py file:

import datetime
from django.shortcuts import render
from incomes.models import Income, Source
from expenses.models import Expense, Category
from .models import SummaryModel


# Create your views here.
def Summary(request):
    todays_date = datetime.date.today()
    six_months_later = todays_date + datetime.timedelta(days=180)
    all_expenses = Expense.objects.filter(owner=request.user, date__gte=todays_date, date__lte=six_months_later)
    all_incomes = Income.objects.filter(owner=request.user, date__gte=todays_date, date__lte=six_months_later)

    def get_amount(EorI):
        amount = 0
        for item in EorI:
            amount += item.amount
        return amount

    final_rep = {'Net_Income' : get_amount(all_incomes) - get_amount(all_expenses)}
    return render(request, 'summary.html', final_rep)

Here is my summary.html file:

<div class="card">
    <div class="card-body">
        </select>
        <div class="form-group">
            <label for="">Choose a date</label>
            <input type="date" value="{{values.date | date:'Y-d-m'}}" class="form-control form-control-sm" name="random_date" />
        </div>

        <input type="submit" value="Submit" class="btn btn-primary btn-primary-sm" />

        <div class="container">
            <div class="col s12 m12 l4">
                <div class="card-panel">
                    <h8 class="bold">Net Budget</h8>
                    <h1 class="bold">${{ Net_Income }}</h1>
                </div>
            </div>
        </div>
    </div>
    </form>
</div>
</div>

Solution

you can access POST or GET parameters from your request like this:

request.GET.get('my_var')
request.POST.get('my_var')

your summary.html sample omits your form opening tag so here is a view you might use assuming that you are POSTing information to your view:

def Summary(request):
    todays_date = datetime.date.today()
    six_months_later = todays_date + datetime.timedelta(days=180)

    random_date = request.POST.get('random_date')

    all_expenses = Expense.objects.filter(owner=request.user, date__gte=todays_date, date__lte=six_months_later)
    all_incomes = Income.objects.filter(owner=request.user, date__gte=todays_date, date__lte=six_months_later)

    context = {'Net_Income' : get_amount(all_incomes) - get_amount(all_expenses)}

    if random_date:
       filtered_expenses = Expense.objects.filter(owner=request.user, date__gte=todays_date, date__lte=random_date)
       filtered_incomes = Income.objects.filter(owner=request.user, date__gte=todays_date, date__lte=random_date)
       context['Filtered_Income'] = get_amount(filtered_incomes) - get_amount(filtered_expenses)

    def get_amount(EorI):
        amount = 0
        for item in EorI:
            amount += item.amount
        return amount

    
    return render(request, 'summary.html', context)

new value will be available as Filtered_Income in your template, but only if the user has POSTed a date so you will need to use something like {% if Filtered_Income %}

Answered By – dafrandle

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published