On Tue, Aug 15, 2006 at 12:47:27PM +0100, Jon Atkinson wrote: > > Hi, > > One of the things which I love about django is the lack of code I have > to write. I recently refactored some of my views from approximately 30 > lines of code down to just two, but I'm worried about the readablity > of my code. > > For the simplest view, I think my code looks fine, and it's pretty readable: > > def index(request, page=0): > paginator = ObjectPaginator(Item.objects.all().order_by('-time'), 15) > return render_to_response('planetx/index.html', {'title': "Planet X", > 'items': paginator.get_page(page)}) > > However, once my views start to get a little more complicated, I end > up with quite long strings of code, which I'm not entirely happy with: > > def feedtype(request, feedtype, page=0): > paginator = > ObjectPaginator(Item.objects.filter(feed__feedtype__feedtype__iexact=re.sub('(s$|S$)', > '', feedtype)).order_by('-time'), 15) > return render_to_response('planetx/feedtypepage.html', {'realname': > "Planet X", 'feedtype': feedtype, 'items': paginator.get_page(page), > 'pages':{'nextpage': paginator.has_next_page(page), 'nextpagenumber': > (page + 1), 'previouspage': paginator.has_previous_page(page), > 'previouspagenumber': (page - 1)}})
I'd do something like: def feedtype(request, feedtype, page=0): paginator = ObjectPaginator( Item.objects.filter( feed__feedtype__feedtype__iexact=re.sub('(s$|S$)', '', feedtype ) ).order_db('-time'), 15 ) return render_to_response( 'planetx/feedtypepage.html', { 'realname': "Planet X", 'feedtype': feedtype, 'items': paginator.get_page(page), 'pages': { 'nextpage': paginator.has_next_page(page), 'nextpagenumber': (page + 1), 'previouspage': paginator.has_previous_page(page), 'previouspagenumber': (page -1) }, } ) Which looks like a lot more - but is just laid out easier and makes it easier to edit later. More lines aren't neccessarily evil :) Not sure that's too pythonic, but it means that I can read it! Cheers, -- Brett Parker --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---