Search code examples
pythondjangoformspostget

Get form returning None - Django 2


I'm trying to filter a view based on a input form. It means that I want to store the input variables and filter the view's model with this.

There's two ways to do this: by POST or by GET. I've tried both of them, but I believe that GET method is more useful.

I don't know if the problem is that I'm not using a form on forms.py or something on urls.py, views.py and the html template.

Anyway. Here is my code:

views.py

class PareceresHistAsset(LoginRequiredMixin, ListView):
    login_url = 'login/'
    redirect_field_name = 'redirect_to'
    template_name = "dash_asset.html"

    # Obtendo o ano anterior ao vigente
    ano = datetime.now().year - 1
    # Model Utilizado
    model = Pareceres
    queryset = Pareceres.objetos.filter(data_comite__year__gte=ano).filter(segmento='asset')

    # Criando os dados de context da view
    def get_context_data(self, **kwargs):
        ## Queryset do objeto
        queryset = self.queryset

        # Teste
        data_inicial = self.request.GET.get('data_inicial')
        print(data_inicial)
...

urls.py

from portal.views import *
from django.urls import path
from django.contrib.auth.views import LoginView

app_name = 'portal'

# urlpatterns contém a lista de roteamento URLs

urlpatterns = [
    # GET /
...
    path('asset/dash', PareceresHistAsset.as_view(), name='dash_asset'),
...
]

dash_asset.html

<div class="card mt-3">
    <div class="card-body">    
        <form method="get">
            <div class="row">
                <!-- Data Inicial-->
                <div class="col-2">
                    <label for="data_inicial">Data Inicial</label>
                    <input id="data_inicial" width="150" name="data_inicial"/>
                </div>
                <!-- Data Final -->
                <div class="col-2">
                    <label for="data_final">Data Final</label>
                    <input id="data_final" width="150" name="data_final"/>
                </div>
                <!-- Botão Submitt-->
                <div class="col-3 text-left">
                    <br>
                    <button class="btn btn-primary btn-sm" id="filtra_datas" type="submit">Atualizar</button>
                </div>
        </form>
        </div>
    </div>
</div>

And the javascript used in the input form

    <!-- Material Design Date Picker -->
    <script src="https://unpkg.com/[email protected]/js/gijgo.min.js" type="text/javascript"></script>
    <link href="https://unpkg.com/[email protected]/css/gijgo.min.css" rel="stylesheet" type="text/css" />


    function formatDate(date) {
        var d = new Date(date),
            month = '' + (d.getMonth() + 1),
            day = '' + d.getDate(),
            year = d.getFullYear();

        if (month.length < 2) month = '0' + month;
        if (day.length < 2) day = '0' + day;

        return [day, month, year].join('/');
    }    

    $('#data_inicial').datepicker({ 
        format:

 'dd/mm/yyyy',
    value: '01/01/2019',
});
$('#data_final').datepicker({
    format: 'dd/mm/yyyy',
    value: formatDate(Date())
});

When I submit the form, only a question mark is added to my url. It came from http://127.0.0.1:8000/asset/dash to http://127.0.0.1:8000/asset/dash? .

Another thing is that variable data_inicial (inside get_context_data) returns none.

Anyone knows how to solve it?

I'm stuck on this for like three days.

Thank you.

Edit: Inserted the name attribute on inputs and corrected the name and id from data_incial to data_inicial.

Still returning None


Solution

  • You haven't given your inputs a name attribute; without that, the browser will not send any data.

    <input id="data_inicial" name="data_inicial" width="150" />
    

    (Also, beware of typos; data_incial vs data_inicial.)