locals()返回一個包含當前作用域里面的所有變量和它們的值的字典。
所以可以把views改寫為
def current_datetime(request): current_date = datetime.datetime.now() return render_to_response('current_datetime.html', locals())
這里要注意的是要把now重命名為current_date,因為模板需要的是這個變量名。
在template是如下定義的:
<html> <body> <font color = "blue">It is is now {{ current_date }}.</font> </body> </html>
之前因為還使用now,結果導致時間在訪問http://localhost:8000/time/
的時候顯示為空白。