object has no attribute 'save' when I try to save form in django

0

Good afternoon friends I have a slight problem, and I do not see the solution, I'm trying to save a search value by rank of fronts in the database using a form, all right, but when I try to save it gives me this error :

'FiltroFechas' object has no attribute 'save'

I've seen some solutions and it's changing the class in form.py to ModelForm but I really do not want to use a model but my own form

I leave you my view.py and my template, thanks in advance

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()
    if request.method == "POST":
        formulario = FiltroFechas(request.POST)
        if formulario.is_valid():
            consulta = formulario.save(commit=False)
            consulta.hasta = timezone.now()
            consulta.save()
    else:
        formulario = FiltroFechas()
        args = {}
        args.update(csrf(request))
        args['form'] = formulario
    return render_to_response('index.html', {'form': formulario, 'porciento':porciento_general, 'general':general, 'plan_gral':plan_gral,'suma':plan_gral['sum'],'juridico':juridic_gral['sum'], 'ingreso':ingreso, 'natural':natural_gral['sum']}, context_instance=RequestContext(request))

index.html

<form action="" method="POST"> {% csrf_token %}
Desde:{{ form.desde }}&nbsp;&nbsp; Hasta:{{ form.hasta }}<br/>
<input  type = "submit" name = "submit" value = "Ver resultados">
</form>

Form.py

# encoding:utf-8 
from django.forms import ModelForm
from django import forms
from principal.models import jovenclub
from functools import partial
DateInput = partial(forms.DateInput, {'class':'form-control bs-datepicker'})

class FiltroFechas(forms.Form):
    desde = forms.DateField(widget=DateInput())
    hasta = forms.DateField(widget=DateInput())
    
asked by Roly Miranda Díaz 04.04.2017 в 22:00
source

1 answer

0

I do not understand where you want to "save" , but there is something that is clear:

Class forms.Form inherits its properties and methods from two classes:

  • DeclarativeFieldsMetaclass - used to obtain the fields that will be used in the form, and
  • BaseForm - which contains the most basic logic of the Django forms. This class provides, among others, the following methods: order_fields , is_valid , add_prefix , as_table , as_ul , as_p . You can check the source code at this address .

What I'm going to do is that, effectively, the class FiltroFechas does not have any method save() where to store the values.

Alternatives

  • If you add the changes to your model, you can save them.
  • Save them to a text file on your server.
  • Save them in the browser's localStorage.
answered by 08.04.2017 / 03:23
source