Django视图函数

一、视图函数 

1. 视图函数的第一个参数一定是一个HTTPRequest类型的对象,这个对象是Django自动创建的,具体形参名通常用request。通过这个对象,可以调用请求的一些参数,比如request.path,request.method,request.META去查看请求信息。

2. 视图函数必须返回一个HTTPResponse类型的对象。(也可以用JsonResponse,StreamingHttpResponse,FileResponse类返回非http对象

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

  实际使用中,通过加载视图模板的方式,将视图函数与前端显示层解耦。

from django.http import HttpResponse
from django.template import loader
from .models import Question

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = {
    'latest_question_list': latest_question_list,
    }
    return HttpResponse(template.render(context, request))

  但是这么写,视图函数的语法比较繁琐,所以引入快捷方式,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)

  render()函数的第一个位置参数是请求对象,第二个位置参数是模板,还可以有一个可选的第三参数---一个字典,包含需要传递给模板的数据。最后render函数返回一个经过字典数据渲染过的模板封装而成的HttpResponse对象。

3. 对于视图函数中操作模型时,如果找不到模型的值会返回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(request, 'polls/detail.html', {'question': question})

  可以使用快捷方式,get_object_or_404()函数和get_list_or_404()函数(用来替代filter()函数,当查询列表为空时弹出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})

  将一个模型作为第一个位置参数,后面可以跟上任意个数的关键字参数(这些关键字参数是传递给模型管理器的get()函数的),如果对象不存在则弹出Http404错误。

4.HTTPResponse类有很多子类,可以返回不同状态的应答。

  HttpResponseRedirect 302

  HttpResponsePermanentRedirect 301

  HttpResponseNotModified 304

  HttpResponseBadRequest 400

    HttpResponseNotFound 404

  HttpResponseForbidden 403

  HttpResponseNotAllowed 405

  HttpResponseGone 410

  HttpResponseServerError 500

例如返回404:

from django.http import HttpResponse, HttpResponseNotFound

def my_view(request):
    # ...
    if foo:
        return HttpResponseNotFound('<h1>Page not found</h1>')
    else:
        return HttpResponse('<h1>Page was found</h1>')

5.由于404错误的应用情景比较多,Django有一套默认处理机制。首先,当发生404异常时,会在根URLconf中查找处理404错误的handler404(并且只能在根URLconf中查找),如果找到了则按设置的错误视图模板显示404页面(还有处理500、403、400的handler)。

handler404 = 'mysite.views.my_custom_page_not_found_view'
handler500 = 'mysite.views.my_custom_error_view'

  如果没有设置handler404,会自动调用内置的django.views.defaults.page_not_found(),这个函数返回一个渲染“Libsite-packagesdjangocontribadmin emplatesadmin404.html”模板的HttpResponseNotFound对象。因此,通常情况下,可以不需要写404错误页面,在视图函数中抛出404异常时,框架会调用自带的404.html;如果需要自定义404页面,则可以在根URLconf中设置指向的自定义404页面,或者可以自编一个404.html文件放在模板树(template tree)的前面(一般是模板的根目录下),屏蔽默认404模板。如果settings里面的DEBUG设置为False,并且不创建404.html文件的话,会出现一个Http500错误,所以创建一个404.html模板文件是很有必要的。如果DEBUG设置为True,那么404view将不会被用到,因此404.html模板也不会被渲染,取而代之的将是浏览器上出现的traceback错误。在django的URLconf中无法匹配任何一个正则表达式时也会调用404页面。

原文地址:https://www.cnblogs.com/pemp/p/6006342.html