Writing my first Django app - 4

2020-07-28  本文已影响0人  游文影月志

1. Writing more views

polls/views.py

def detail(request, questionId):
    return HttpResponse(f'You\'re looking at question {questionId}')


def results(request, questionId):
    return HttpResponse(f'You\'re looking at the results of question {questionId}')


def vote(request, questionId):
    return HttpResponse(f'You\'re voting on question {questionId}')

Wire these new views into the polls.urls module by adding the following path() calls:

polls/urls.py

urlpatterns = [
    path('', views.index, name='index'),
    path('<int:questionId>/', views.detail, name='detail'),
    path('<int:questionId>/results', views.results, name='results'),
    path('<int:questionId>/vote/', views.vote, name='vote'),
]

Visit http://127.0.0.1:8000/polls/12 with the web browser. It'll run the detail() method and display a sentence: "You're looking at question 12". Try /polls/34/results/ and /polls/34/vote/ will display the placeholder results and voting pages.

2. Write views that actually do something

上一篇 下一篇

猜你喜欢

热点阅读