USEGEAR

导航

学习Django【2】把视图和路由连接起来的是URLconfigs的配置文件

在view.py视图中定义:

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


def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)


def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

然后在urls.py中注明:

from django.urls import path

from . import views

urlpatterns = [
    # ex: /polls/
    path("", views.index, name="index"),
    # ex: /polls/5/
    path("<int:question_id>/", views.detail, name="detail"),
    # ex: /polls/5/results/
    path("<int:question_id>/results/", views.results, name="results"),
    # ex: /polls/5/vote/
    path("<int:question_id>/vote/", views.vote, name="vote"),
]

在浏览器中

 就可以看到效果了。

 

posted on 2024-06-01 08:19  USEGEAR  阅读(8)  评论(0编辑  收藏  举报