美文网首页
Writing my first Django app - 4

Writing my first Django app - 4

作者: 游文影月志 | 来源:发表于2020-07-28 05:45 被阅读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

相关文章

网友评论

      本文标题:Writing my first Django app - 4

      本文链接:https://www.haomeiwen.com/subject/ydexrktx.html