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
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
- [Django]-How do you serialize a model instance in Django?
- [Django]-Access request in django custom template tags
- [Django]-Can "list_display" in a Django ModelAdmin display attributes of ForeignKey fields?
6๐
Yes, the old function-based generic views were deprecated in 1.4. Use the class-based views instead.
๐คDaniel Roseman
- [Django]-Django REST Framework : "This field is required." with required=False and unique_together
- [Django]-How can I avoid "Using selector: EpollSelector" log message in Django?
- [Django]-.filter() vs .get() for single object? (Django)
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
- [Django]-Django Rest Framework โ no module named rest_framework
- [Django]-How can I save my secret keys and password securely in my version control system?
- [Django]-Django REST Framework custom fields validation
Source:stackexchange.com