本棚アプリケーションの作成②【5-2 トップページの作成】

はじめに

トップページを作成していきます。

まずはurls.pyとviews.pyファイルを編集していきます。

bookproject/book/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('book/', views.ListBookView.as_view(),
name='list-book'),
    path('book/<int:pk>/detail/', views.DetailBookView.as_view(),
name='detail-book'),
    path('book/create/', views.CreateBookView.as_view(),
name='create-book'),
    path('book/<int:pk>/delete/', views.DeleteBookView.as_view(),
name='delete-book'),
    path('book/<int:pk>/update/', views.UpdateBookView.as_view(),
name='update-book')
]

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index_view, name='index'),
    path('book/', views.ListBookView.as_view(), name='list-book'),
    path('book/<int:pk>/detail/', views.DetailBookView.as_view(),
name='detail-book'),
    path('book/create/', views.CreateBookView.as_view(), name='create-book'),
    path('book/<int:pk>/delete/', views.DeleteBookView.as_view(),
name='delete-book'),
    path('book/<int:pk>/update/', views.UpdateBookView.as_view(),
name='update-book')
]

に編集

bookproject/book/views.py

from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import (ListView, DetailView, CreateView, DeleteView,
UpdateView)
from .models import Book

class ListBookView(ListView):
    template_name = 'book/book_list.html'
    model = Book

class DetailBookView(DetailView):
    template_name = 'book/book_detail.html'
    model = Book

class CreateBookView(CreateView):
    template_name = 'book/book_create.html'
    model = Book
    fields = ('title','text','category')
    success_url = reverse_lazy('list-book')

class DeleteBookView(DeleteView):
    template_name = 'book/book_confirm_delete.html'
    model = Book
    success_url = reverse_lazy('list-book')

class UpdateBookView(UpdateView):
    model = Book
    fields = (['title','text','category'])
    template_name = 'book/book_update.html'
    success_url = reverse_lazy('list-book')

from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import (ListView, DetailView, CreateView, DeleteView,
UpdateView)
from .models import Book

class ListBookView(ListView):
    template_name = 'book/book_list.html'
    model = Book

class DetailBookView(DetailView):
    template_name = 'book/book_detail.html'
    model = Book

class CreateBookView(CreateView):
    template_name = 'book/book_create.html'
    model = Book
    fields = ('title','text','category')
    success_url = reverse_lazy('list-book')

class DeleteBookView(DeleteView):
    template_name = 'book/book_confirm_delete.html'
    model = Book
    success_url = reverse_lazy('list-book')

class UpdateBookView(UpdateView):
    model = Book
    fields = (['title','text','category'])
    template_name = 'book/book_update.html'
    success_url = reverse_lazy('list-book')

def index_view(request):
    return render(request, 'book/index.html',{'somedata':100})

に編集

renderについて

index.htmlファイルを作成していきます。

(venv) $ touch book/templates/book/index.html

そして以下のコードを書き込んでいきます。

bookproject/book/templates/book/index.html

{{ somedata }}

そしてサーバーを立ち上げてhttp://127.0.0.1:8000/にアクセスします。

(venv) $ python manage.py runserver 

ターミナルに表示させる

function-based viewにおけるDjangoの内部処理についてより詳しく見ていきます。

views.pyファイルに次のコードを追記します。

bookproject/book/views.py

・・・省略・・・
def index_view(request):
    return render(request, 'book/index.html',{'somedata':100})
・・・省略・・・

・・・省略・・・
def index_view(request):
    print('index_view is called')
    return render(request, 'book/index.html',{'somedata':100})
・・・省略・・・

に編集

そして再度サーバーを立ち上げてhttp://127.0.0.1:8000/にアクセスします。

(venv) $ python manage.py runserver 

そしてwebサイトのほうではなく、ターミナルを見ると、いままでサーバーを立ち上げたときのターミナルは下の画像で

今回サーバーを立ち上げたときのターミナルはこちらになります。