[Answered ]-The smartest way to redirect in django without creating view

1👍

You could use reverse_lazy (Django 1.4) in you url configuration, like so:

from django.conf.urls.defaults import url, patterns
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import redirect

urlpatterns = patterns('',
    url(r'^/$', lambda request: return redirect(reverse_lazy('url_name')),
)

Another possibility is to define LOGIN_URL using reverse_lazy, so you could continue to use settings.LOGIN_URL in your redirects.

Code is untested, might have a typo somewhere.

👤Bouke

1👍

You just need to mixin LoginRequired to your view. You can find an example of the mixin here:

http://djangosnippets.org/snippets/2442/

Then where you define that view, you just do:

class RedirectView(LoginRequiredMixin, DetailView):
    ....

Or whatever Class Based View you’re inheriting from. Hope that helps!

Leave a comment