M
M
Mooris2017-05-11 22:35:46
Django
Mooris, 2017-05-11 22:35:46

Django how to display object only after filtering?

Hello, I need your help. In this code, all objects from the model are displayed on the page, there is a form by which they are filtered. It is necessary for me that all objects are not initially displayed, but appear only after filtering.
views.py

def abonstat(request):
    abons = Uslugi.objects.all()
    abform = AbonStatForm(request.GET)
    if abform.is_valid():
        if abform.cleaned_data['client']:
            abons = abons.filter(client_id=abform.cleaned_data['client'])
    return render(request, 'clients/abonstat.html',
                  {'abons': abons, 'abform': abform})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-05-11
@Mooris

?

def abonstat(request):
    abons = {}
    abform = AbonStatForm(request.GET)
    if abform.is_valid():
        if abform.cleaned_data['client']:
            abons = Uslugi.objects.filter(
                            client_id=abform.cleaned_data['client'])
    return render(request, 'clients/abonstat.html',
                  {'abons': abons, 'abform': abform})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question