Filter Models in the Django view

Asked

Viewed 247 times

0

Fala Galera

I have the following app running on Django 1.6.

inserir a descrição da imagem aqui when clicking on dog or cat the idea would be to present a page performing object filtering

The animal is shaped like this to filter:

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

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

How would my view manipulate this kind of display ?

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

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’ll 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 his TIPO_CHOICES what is saved in the bank is the first value, the second it is used only for display.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.