码迷,mamicode.com
首页 > 编程语言 > 详细

Python转发与重定向

时间:2020-06-08 23:42:36      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:重定向   返回   render   一个   size   contex   end   转发   style   

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

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 ------))

 

 

 

 

Python转发与重定向

标签:重定向   返回   render   一个   size   contex   end   转发   style   

原文地址:https://www.cnblogs.com/tracydzf/p/13069079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!