📚
Django開発入門7:モデルからデータを取り出す
データを取り出す
bbs/views.py
from django.shortcuts import render
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)
テンプレートを変える
bbs/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>paiza bbs</title>
<style>body {padding: 10px;}</style>
</head>
<body>
<h1>paiza bbs</h1>
<p>{{ message }}</p>
{% for article in articles %}
<p>{{ article.content }}</p>
{% endfor %}
</body>
</html>
Discussion