Home  >  Article  >  Database  >  Django:locals()小技巧

Django:locals()小技巧

WBOY
WBOYOriginal
2016-06-07 14:59:451102browse

locals()返回一个包含当前作用域里面的所有变量和它们的值的字典。 所以可以把views改写为 def current_datetime(request): current_date = datetime.datetime.now() return render_to_response('current_datetime.html', locals()) 这里要注意的是要把now重

locals()返回一个包含当前作用域里面的所有变量和它们的值的字典。

所以可以把views改写为

def current_datetime(request):
    current_date = datetime.datetime.now()
    return render_to_response('current_datetime.html', locals())

 

这里要注意的是要把now重命名为current_date,因为模板需要的是这个变量名。

在template是如下定义的:

    
        <font color="blue">It is is now {{ current_date }}.</font>
    

 

之前因为还使用now,结果导致时间在访问http://localhost:8000/time/

的时候显示为空白。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn