Show date filter data in Django

0

What I want is to make a data filter and show values according to dates.

To make evaluations, monthly, quarterly and so on.

To explain myself better, I leave my view.py and my form.py

form.py

class FiltroFechas(forms.ModelForm):

    class Meta:
        model = ingresos
        fields = ['desde', 'hasta', 'jovenclub']
        widgets = {
             'desde': forms.DateInput(format=('%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y',), attrs={'class': 'form-control bs-datepicker', 'placeholder': 'Seleccione la fecha Correcta'}),
             'hasta': forms.DateInput(format=('%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y',), attrs={'class': 'form-control bs-datepicker', 'placeholder': 'Seleccione la fecha Correcta'})
        }

View.py

 def inicio(request):
    plan_gral = jovenclub.objects.aggregate(sum=Sum('plan_gral'))
    juridic_gral = ingresos.objects.aggregate(sum=Sum('juridico'))
    natural_gral = ingresos.objects.aggregate(sum=Sum('natural'))
    general = juridic_gral['sum']+natural_gral['sum']
    porciento_general = general*100/plan_gral['sum']
    ingreso = ingresos.objects.all()
    form = FiltroFechas(request.POST)


    if form.is_valid():
        desde = request.POST.get("desde")
        desde_a_datetime = datetime.strptime(desde, '%d/%m/%Y')
        # hasta = request.POST.get('hasta')
        hasta = request.POST.get("hasta")
        hasta_a_datetime = datetime.strptime(hasta, '%d/%m/%Y')
        filtro=ingresos.objects.filter(fecha__range=(desde_a_datetime, hasta_a_datetime))

        print filtro
        return render_to_response('index.html', {'form':form, 'resultado':filtro})

    return render_to_response('index.html', {'porciento':porciento_general, 'form':form, 'general':general, 'plan_gral':plan_gral,'suma':plan_gral['sum'], 'juridico':juridic_gral['sum'], 'ingreso':ingreso, 'natural':natural_gral['sum']}, context_instance=RequestContext(request))
    
asked by Roly Miranda Díaz 10.04.2017 в 22:03
source

1 answer

1

You must render is the content of your variable resultado which is how you declare when using render_to_response and it is also the one that contains the values that you filter in your view using the variable filtro , since form is an instance of your form and does not contain the values that you extract according to your rank, therefore you must have something like the following in your template:

{% for datos in resultado %}
    {{datos.fecha}}
{% endfor %}

In the same way, I recommend that you can initialize your filter variable before form.is_valid() :

filtro = None

So that you have a single return in your view, you can use locals() instead of creating a dict with each of the variables you want to return

return render_to_response('index.html', locals(), context_instance=RequestContext(request))

I hope it's helpful

    
answered by 10.04.2017 / 22:12
source