Hello people, my ongoing adventures in firstdjangoappland continue thus:

I'm putting together a simple search app for a car project. On entry,
user sees a list of 5 random cars. He can use up to two comboboxes to
filter the results (car model and fuel type).

This is what I've come up with on my home() view, so far, and it looks
really ugly.

def home(request):

        results = {}

        # these 2 guys are the filters
        model_list = Model.objects.all()
        gorivo_list = Gorivo.objects.all().order_by('opis')
        
        selected_model_id = 0
        selected_gorivo_id = 0
        
        if request.POST:
                # this handles 'SELECTED' attribute on comboboxes
                try:
                        selected_model_id = int(request.POST['model_id'])
                except:
                        pass
                try:
                        selected_gorivo_id = int(request.POST['gorivo_id'])
                except:
                        pass

                # and this should take care of the filtering... but I'm stumped 
really
                # both filters should add up
                results = Vozilo.objects.all()
                if selected_model_id != 0:
                        results = 
results.filter(model_id__exact=selected_model_id)
                if selected_gorivo_id != 0:
                        results = 
results.filter(gorivo_id__exact=selected_gorivo_id)
        else:
        # no POST, we show 5 random cars.
                results = Vozilo.objects.order_by('?')[:5]

        return render_to_response('cars/home.html', {
                'model_list':model_list,
                'gorivo_list':gorivo_list,
                'selected_model_id':selected_model_id,
                'selected_gorivo_id':selected_gorivo_id,                
                'results': results
        })


There must be a better way of doing this, right? I'm a terrible newbie
both at Python and Django --and I'm not a kid anymore, so no more time
to spare hitting my head on walls :-)

Insights? Ideas?

Thanks a million,

-- 
Carlos Yoder
http://carlitosyoder.blogspot.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to