Django/bbs

//myapp/bbs/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('', views.detail, name='detail'),
]
    

//myapp/bbs/views.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)
    context = {
        'message': 'Show Article ' + str(id),
        'article': article,
    }
    return render(request, 'bbs/detail.html', context)



//myapp/bbs/templates/bbs/index.html

!DOCTYPE html>
html>
    head>
        meta charset='utf-8'>
        title>bbs/title>
    /head>
    body>
        h1>bbs/h1>
        p>{{ message }}/p>
        {% for article in articles %}
		   p>
		       {{ article.content }}
		       [a href="{% url 'detail' article.id %}">詳細/a>]
		   /p>
		{% endfor %}
    /body>
/html>


//myapp/bbs/templates/bbs/detail.html
body>
    p>{{ message }}/p>
    p>{{ article.content }}/p>
    p>a href="{% url 'index' %}">一覧/a>/p>
/body>
    
    
python manage.py runserver

//ブラウザ
http://localhost:8000/bbs