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