DEV Community

kaede
kaede

Posted on

Django Tutorial Part 5 -- polls アプリのページを追加

https://docs.djangoproject.com/en/4.0/intro/tutorial03/#writing-more-views

現在の urls と views を確認

polls/urls.py

urlpatterns = [
    path('', views.index, name='index'),
]
Enter fullscreen mode Exit fullscreen mode

polls/views.py

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")
Enter fullscreen mode Exit fullscreen mode
  • urlspatterns に polls の root から index への誘導
  • def index に index へのメッセージ

これらしか書いてない。よって

Image description

現在 polls アプリで見れるのが index ページのこのメッセージしかない。
なので

  • details
  • results
  • votes

のページを urls と views に追加作成して、ページを増やす。


details, results, votes のページを作成する

urlpatterns = [
    path('', views.index, name='index'),
    # ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
]
Enter fullscreen mode Exit fullscreen mode

polls/question_id/ で views の detail に飛んで

def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)
Enter fullscreen mode Exit fullscreen mode

views の detail で question_id を表示する

Image description

    path('<int:question_id>/results', views.results, name='results'),
    path('<int:question_id>/votes', views.votes, name='votes'),
Enter fullscreen mode Exit fullscreen mode
def results(request, question_id):
    return HttpResponse("You're looking resutls of %s." % question_id)
def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

results, vote も同じように作る

Top comments (0)