HomeAbout

Routing

Dynamic Routing

Pass URL params to Views

# urls.py from django.urls import path from . import views urlpatterns = [ path('articles/<int:year>/', views.year_archive, name='year-archive'), path('articles/<int:year>/<int:month>/', views.month_archive, name='month-archive'), path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail, name='article-detail'), ] # views.py from django.shortcuts import render from .models import Article # models.py def year_archive(request, year): articles = Article.objects.filter(publish_date__year=year) return render(request, 'articles/year_archive.html', {'articles': articles, 'year': year}) def month_archive(request, year, month): articles = Article.objects.filter(publish_date__year=year, publish_date__month=month) return render(request, 'articles/month_archive.html', {'articles': articles, 'year': year, 'month': month}) def article_detail(request, year, month, slug): article = Article.objects.get(publish_date__year=year, publish_date__month=month, slug=slug) return render(request, 'articles/article_detail.html', {'article': article})
AboutContact