On Sat, May 23, 2009 at 4:56 AM, Nicolas Steinmetz <nsteinm...@gmail.com> wrote:
>
> Hi,
>
> I was thinking about something like the following in urls.py :
>
>     (r'(?P<shortcut>\w+)', 'django.views.generic.simple.redirect_to',
> {'url': Shorturl.objects.get(slug=shortcut).url }),
>
> But I did not manage to make it work.
>
> In the meanwhile I did this which works but I'm a little "disappointed"
> with this :
>
> urls.py:
> --------
>
> from django.conf.urls.defaults import *
> from shorturl.views import redirect_to_url
>
> urlpatterns = patterns('',
>     (r'(?P<shortcut>\w+)',  'shorturl.views.redirect_to_url'),
> )
>
> views.py:
> ---------
>
> from django.http import HttpResponsePermanentRedirect
> from atome.shorturl.models import Shorturl
>
> def redirect_to_url(request, shortcut):
>     return
> HttpResponsePermanentRedirect(Shorturl.objects.get(slug=shortcut).url)
>
> Any idea ?

You could probably genericize your redirect_to_url view a little bit
more - you could make the class that is used for lookup configurable,
for example - but you have essentially hit on the solution.

The issue is that 'shortcut', as captured by the regular expression,
isn't a Python variable you can use in the .get() query. As a result,
the only way you're going to be able to use shortcut in a query is to
push the query into an actual function call - or write a generic view
of your own that will perform that function call, based upon a known
set of named arguments in the pattern.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to