view 是 Django 的逻辑层 — 一个函数或类,它接收 HTTP request,执行逻辑(查询模型、处理输入),并返回 HTTP response。Views 是你处理当 URL 被访问时发生什么的地方。
Function-based views (FBVs)
python
django.shortcuts render, get_object_or_404
django.http JsonResponse, HttpResponse
():
articles = Article.objects.()
render(request, , {: articles})
():
article = get_object_or_404(Article, pk=pk)
render(request, , {: article})
():
data = (Article.objects.values(, ))
JsonResponse(data, safe=)
