https://docs.djangoproject.com/en/2.1/intro/tutorial03/
在polls/views.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)
视图函数需要和URLconf相对应,所有接下来编写URLconf(在polls/urls.py中)
备注:
- path()函数的第一个参数是URLconf,第二个参数是该URLconf所调用的视图函数,name参数命名一个名字
- path()函数的第一个参数中 <int:question_id> 表示用户请求变量,int是变量类型,然后用冒号分隔,question_id是传递给视图函数的参数。
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
现在访问地址127.0.0.1:8000/polls/18/,127.0.0.1:8000/polls/23/results/,127.0.0.1:8000/polls/16/vote/,
会在浏览器中看到Django分别调用了不用的视图函数。
创建一个可以调用数据模型(数据库中的数据)的视图函数
from django.http import HttpResponse
from .models import Question
def index(request):
## 提取5个Question对象(按日期排序,最新的5个)
question_object_list = Question.objects.order_by('pub_date')[:5]
## 只选取Question对象中的question_text(相当于数据库表中的question_text字段)
text_list = [q.question_text for q in question_object_list]
out_put = ''.join(text_list)
return HttpResponse(out_put)
视图函数除了应该调用数据模型外,还应该使用模板(这样才真正做到MVC分离)
现在从新改写index视图函数
from django.template import loader
from django.http import HttpResponse
from .models import Question
def index(request):
## 提取5个Question对象(按日期排序,最新的5个)
latest_question_list = Question.objects.order_by('-pub_date')[:5]
## 调用模板,(模板文件polls/index.html还没创建)
template = loader.get_template('polls/index.html')
## 将模板对象映射到python的字典中
context = {'latest_question_list': latest_question_list,}
## 调用模板对象的render方法
return HttpResponse(template.render(context, request))
除了使用HttpResponse对象来对请求作出响应之外,render也能对请求作出响应,而且使用方法更简洁
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list,}
return render(request, 'polls/index.html', context)
视图函数引发404错误
现在来改写detail函数,引发一个404错误
from django.http import Http404
from django.shortcuts import render
from .models import Question
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404('Question does not exist')
return render(requests, 'polls/detail.html', {'question':question})
另一个简洁的方式是使用get_object_or_404方法
from django.shortcuts import get_object_or_404, render
from .models import Question
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
总结:
视图函数应该返回一个HttpResponse或者一个如Http404的异常。
要使视图函数做一些真正做一些事情,视图函数应该能够调用 数据模型 和 模板。
分别对应的方法为
- get_object_or_404(Question, pk=question_id) 调用数据模型或返回Http404
- render(request, ‘polls/detail.html’, {‘question’: question}) 调用模板,并返回HttpResponse