Django的view中常用return方法

第一种HttpResponse,最基本的返回方式,可以直接返回字符:

from django.http import HttpResponse
def index(request):
    return HttpResponse(a test)

或者结合contextloder返回网页:

from django.template import Context, loader  
from polls.models import Poll
def index(request):  
    latest_poll_list = Poll.objects.order_by('-pub_date')[:5]  
    template = loader.get_template('polls/index.html')
    context = Context({ 'latest_poll_list': latest_poll_list, })  
    return HttpResponse(template.render(context))

第二种,可以说是上一种的精简版:

from django.shortcuts import render_to_response
from django.template import RequestContext
def search_view_bak(request, template="shop/search.html"):
    context = RequestContext(request, {
            'results': results,
            'category' : category,
            'keywords' : keywords})
    return render_to_response(template, context)

第三种,最最精简版:

from django.shortcuts import render
def index(request):
    return render(request, "search/search.html", {})

第四中,直接返回其他url:

from django.http import  HttpResponseRedirect
def index(request):
    return HttpResponseRedirect('/home/')