转发:只有一次请求和响应
render
render(request,template_name[,context])
结合一个给定的模板和一个给定的上下文字典,并返回一个渲染后的HttpResponse对象
request:该request用于生成response
template_name:要使用的模板的完整名称
context:添加到模板上下文的一个字典,视图将在渲染模板之前调用它
defindex(request): returnrender(request,'booktest/index.html',{'h1':'hello'})fromdjango.shortcutsimportrender
重定向:两对以上的请求和响应
redirect
redirect(to)
为传递进来的参数返回HttpResponseRedirect
to推荐使用反向解析
fromdjango.shortcutsimportredirect fromdjango.core.urlresolversimportreverse defindex(request): returnredirect(reverse('booktest:index2'))#Python学习交流QQ群:778463939
注意
redirect(reverse(-----这里面写的是一个url------))