Filtering models in Django view

0

Talk Galera

I have the following app running in Django 1.6.

when clicking on dog or cat the idea would be to present a page doing the filtering of the object

The animal is modeled thus to filter:

    TIPO_CHOICES = (
    (1, 'Cachorro'),
    (2, 'Gato')
)

tipo = models.IntegerField('Tipo', max_length=1, choices=TIPO_CHOICES)

How would my view handle this type of view?

def cao(request):
    cao = Animal.objects.filter(tipo__equal = Cachorro)
    template_name = 'Adota/cao.html'
    context = {
        'cao':cao
    }
    return render(request, template_name, context)

Thanks for the support, remembering that I am in django 1.6

    
asked by anonymous 12.04.2018 / 21:03

1 answer

0

The simplest way for you to do this is to create different URLs and views for each of them.

Let's say you get the URL /cao/ and /gato/ . After that you would also have the views cao and gato .

def cao(request):
    caes = Animal.objects.filter(tipo=1)
    # ai aqui vai o resto do código que você já tem

def gato(request):
    gatos = Animal.objects.filter(tipo=2)
    # ai aqui vai o resto do código que você já tem

Remembering that there I put 1 because in its TIPO_CHOICES what is saved in the bank is the first value, the second it is used only for display.

    
14.04.2018 / 19:33