Python Django view render返回参数

两种返回值的方法

1.

def index(request):

    t=time.ctime

return render(request,'index.html',{'time':t})  -->> 在html文件中使用{{time}}调用 -->> time 为key名称



2.

def index(request):

    t=time.ctime

return render(request,'index.html',locals())   -->>在html文件中使用{{t}}调用 -->>t为本地变量名称

                                                         ↑

                                                    获取本地变量

第二种方法避免了有很多变量时的字典填写很长




猜你喜欢

转载自blog.csdn.net/qq_41823953/article/details/79901921
今日推荐