65
ImportError at /
No module named simple

Django Version: 1.5.dev20120710212642

I installed latest django version. I am using

from django.views.generic.simple import redirect_to

in my urls.py. What is wrong? Is it deprecated?

Burak
  • 5,706
  • 20
  • 70
  • 110

4 Answers4

136

Use class-based views instead of redirect_to as these function-based generic views have been deprecated.

Here is simple example of class-based views usage

from django.conf.urls import patterns, url, include
from django.views.generic import TemplateView

urlpatterns = patterns('',
    (r'^about/', TemplateView.as_view(template_name="about.html")),
)

Update

If someone wants to redirect to a URL, Use RedirectView.

from django.views.generic import RedirectView

urlpatterns = patterns('',
    (r'^one/$', RedirectView.as_view(url='/another/')),
)
Ahsan
  • 11,516
  • 12
  • 52
  • 79
53

this should work

from django.conf.urls import patterns
from django.views.generic import RedirectView

urlpatterns = patterns('',
    url(r'some-url', RedirectView.as_view(url='/another-url/'))
)
Adrian Mester
  • 2,523
  • 1
  • 19
  • 23
  • 1
    Thanks for this example. We just upgraded to 1.5 and instantly got errors with both redirect_to and direct_to_template. Upvoted. – commadelimited Feb 27 '13 at 14:47
  • 2
    Thank you! works like a charm! Actually, THIS is the correct answer to the question. The accepted answer is not a replacement for redirect_to. – Simon Steinberger Mar 27 '13 at 06:50
6

Yes, the old function-based generic views were deprecated in 1.4. Use the class-based views instead.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
5

And for the record (no relevant example currently in documentation), to use RedirectView with parameters:

from django.conf.urls import patterns, url
from django.views.generic import RedirectView


urlpatterns = patterns('',
    url(r'^myurl/(?P<my_id>\d+)$', RedirectView.as_view(url='/another_url/%(my_id)s/')),
)

Please note that although the regex looks for a number (\d+), the parameter is passed as a string (%(my_id)s).

What is still unclear is how to use RedirectView with template_name in urls.py.

Wtower
  • 18,848
  • 11
  • 103
  • 80