On 8/15/06, Jon Atkinson <[EMAIL PROTECTED]> 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)}})
>
> Does anyone have any advice about how I should be laying out these
> views? Should I be doing things in a more long-hand (but easier to
> understand) manner? I know that 'pythonic' has quite a nebulous
> definition, but something seems 'unpythonic' about this code, which is
> quite a contrast to the elegance of the rest of the django code I have
> written. In particular, my need to pass the 'pages' tuple to the
> template seems quite clunky; I realise that I may be using the
> ObjectPaginator incorrectly here - if so I'd love to know how I can
> improve this.
>
> Any ideas or tips on style would be appreciated.
>
> --Jon
>
In Woodlog(www.djangocn.org) I encapsulate pagenator to a object, so I
don't need to pass too many variable to template, and just use this
object.

-- 
I like python!
My Blog: http://www.donews.net/limodou
My Django Site: http://www.djangocn.org
NewEdit Maillist: http://groups.google.com/group/NewEdit

--~--~---------~--~----~------------~-------~--~----~
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