👏
Django開発入門9:データの詳細を表示する
ページを増やす
bbs/urls.py
from django.urls import path
from . import views
app_name = 'bbs'
urlpatterns = [
path('', views.index, name='index'),
path('<int:id>', views.detail, name='detail'),
]
データのIDによってページを表示する
get_object_or_404関数は、指定IDのデータがあればページを、なければ404ページを出す。
bbs/urls.py
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from .models import Article
def index(request):
articles = Article.objects.all()
context = {
'message': 'Welcome my BBS',
'articles': articles,
}
return render(request, 'bbs/index.html', context)
def detail(request, id):
article = get_object_or_404(Article, pk=id)
return HttpResponse(article)
Discussion