Python-未定义全局名称“日期” [英] Python - Global name 'date' is not defined
问题描述
我正在Django视图中使用它。我完全复制了此内容: http:/ /www.pythonexamples.org/2010/12/23/how-to-get-todays-date-in-python/
I'm using this inside a Django view. I copied this exactly: http://www.pythonexamples.org/2010/12/23/how-to-get-todays-date-in-python/
即:
import datetime
variable = datetime.date.today()
我也尝试过
from datetime import datetime
variable = datetime.date.today()
但是随后发生此错误:
'method_descriptor' object has no attribute 'today'
推荐答案
您询问了为什么会发生这种情况...
,所以这是我的尝试,也是我在堆栈溢出中的第一个答案。
You asked about why this thing happens... so here goes my try and my first ever answer in stack overflow.
对我来说,这似乎是一个范围/命名空间问题。
It seems like a scope/namespace issue to me.
检查today()的范围。
是否可以从您引用的地方访问它?
Check today() 's scope. Is it accessible from where you refer to it?
也请检查此人:
http://igotgenes.blogspot.gr/2009/01/class-attributes-and-scoping-in -python.html
然后:
https://docs.python.org/2/tutorial/classes.html
对不起,但无济于事,因为我没有您的整个项目可以使用:(
Sorry but can't help more, since I don't have your whole project at my disposal :(
编辑:只需输入一些错字即可。
Just a few typos.
这篇关于Python-未定义全局名称“日期”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!