[Answer]-Redirect django URL to particular app

1👍

Use RedirectView

from django.views.generic.base import RedirectView
urlpatterns = patterns('',
    url(r'^$', RedirectView.as_view(pattern_name='blog'), name='blog_view'),
)

Update: By default RedirectView return 301. So recommend to include permanent

url(r'^$', RedirectView.as_view(pattern_name='blog', permanent=False), \
    name='blog_view'),

0👍

Django 4.2: You can avoid editing views.py files by modifying urls.py in route directory as below:

from django.contrib import admin
from django.urls import path, include, re_path

from django.http import HttpResponseRedirect
def redirect_view(request):
    return HttpResponseRedirect('/blog/')

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include("blog.urls")),
    path('', redirect_view, name='redirect_view'),
    re_path(r'^.+', redirect_view, name='redirect_view')
]

Leave a comment