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'.

Alex

On Mar 26, 8:59 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> Hi Alex,
>
>
>
> On Sun, 2007-03-25 at 17:55 -0700, Alex Dong wrote:
> > Hi folks,
> > I'd like to refactor the following code into one line in urls.py:
>
> > Now:
> > in `urls.py`:
> > +    (r'^watchlist/$',
> > 'package.watchlist.views.rredirect_to_personalized_watch_list'),
> > +    (r'^users/(?P<username>.*)/watchlist/$',
> > 'package.watchlist.views.watchlist'),
>
> > in `views.py`:
> > [EMAIL PROTECTED]
> > +def redirect_to_personalized_watch_list(request):
> > +    return HttpResponseRedirect('/users/%s/watchlist/' %
> > request.user.username)
>
> > I'm wondering how can I eliminate the need for the
> > `redirect_to_personalized_watch_list` and use django's `redirect_to`
> > generic view?  It'll be much easier if I could get `request` object in
> > the `urls.py`.
>
> You can't do this directly because of the different scopes the lines of
> code are executed in. Any solution would involve creating a "delayed
> execution" scope to extract the request.user variable, which is exactly
> what you've done. So either you have a short function as you've got, or
> Django would have to have a short function that does exactly the same
> thing. Same number of lines of code in either case and more flexible the
> current way.
>
> If you really want to put all the logic in one file, there's no reason
> your redirect function has to be in a file other than urls.py. However,
> it looks fine as you've got it now and I wouldn't be in a hurry to move
> it.
>
> 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 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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to