Python转发与重定向

转发:只有一次请求和响应

render

render(request, template_name[, context])
结合一个给定的模板和一个给定的上下文字典,并返回一个渲染后的HttpResponse对象
request:该request用于生成response
template_name:要使用的模板的完整名称
context:添加到模板上下文的一个字典,视图将在渲染模板之前调用它
from django.shortcuts import render

def index(request):
    return render(request, 'booktest/index.html', {'h1': 'hello'})

重定向:两对以上的请求和响应

redirect

redirect(to)
为传递进来的参数返回HttpResponseRedirect
to推荐使用反向解析
from django.shortcuts import redirect
from django.core.urlresolvers import reverse

def index(request):
    return redirect(reverse('booktest:index2'))

注意

 redirect(reverse(-----这里面写的是一个url ------))

 

 

 

 

posted @ 2020-06-08 22:37  Tracydzf  阅读(717)  评论(0编辑  收藏  举报