Redirecting the Homepage
Set up the homepage to redirect to the articles list — configure the root URL to show articles instead of a blank welcome page.
Redirecting the Homepage
Right now the homepage (/) shows a basic welcome page. Let us make it redirect to the articles list — or render the article list directly.
Option 1: Redirect
# djangoblog/views.py
from django.shortcuts import redirect
def homepage(request):
return redirect('articles:list')
When users visit /, they are immediately sent to /articles/.
Option 2: Render Articles at Root
# djangoblog/urls.py
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('articles.urls')), # Articles at root
path('accounts/', include('accounts.urls')),
]
This serves the articles list at / instead of /articles/. Article detail pages would then be at /my-article-slug/.
Option 3: RedirectView
from django.views.generic import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('', RedirectView.as_view(url='/articles/')),
path('articles/', include('articles.urls')),
path('accounts/', include('accounts.urls')),
]
Key Takeaways
redirect('name')sends users to a named URL- Use
include()at the root path to serve articles directly at/ RedirectViewis a class-based alternative for simple redirects- Choose the approach that best fits your URL structure