On Sun, 2007-03-25 at 18:22 -0700, Alex Dong wrote: > Hi Malcolm, > > Thanks for the quick reply. > > What I'm wondering about is that I've seen the pattern repeat itself > quite a few times where I have to write a little wrapper doing nothing > more than retrieving something from the `request` object. > Like > * request.user.get_home_url > * request.user.get_watch_list_url > * request.user.private_message > ... > > So my views.py file ends up with lots of this one line methods, which > I found a little bit 'unclean'.
So write one function and one of the parameters you pass it is the name of the "thing" to get, using getattr() to get the item. You can pass in static parameter values as part of the url configuration (the optional third argument after the reg-exp and function). However that wasn't what your original example was doing at all. I'm not really sure what answer you are expecting here. You have to extract the user information at view processing time -- it can't be done any earlier because that is the only time you have the accurate request object. So you need to have views. Small functions that do slightly different things aren't untidy, or, at least, not universally so. Just try to factor out the common stuff, using all the power that Python give you. Your original example -- and similar things -- could be done with a decorator that extracts the request.user and tacks it onto the parameters passed to your view. You would need to decide to have a consistent name for that parameter. The (different) example you posted above can be handled by realising the only difference is the attribute you are retrieving, so using getattr() solves that. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

