AttributeError:即使使用导入日期时间后,类型对象“ datetime.datetime”也没有属性“ datetime”

时间:2019-10-04 11:21:44

标签: python datetime flask

我在flask项目中有以下代码可以修改时间戳。我没有进行任何修改,但是代码现在引发了上述错误。

我已经在StackOverflow上提到了所有类似的问题,但都表明这是由于使用from datetime import datetime而不是import datetime。但是在下面的代码中,我正在使用import datetime。但是,我仍然得到AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

我的代码:

view.py

import datetime


@app.template_filter('datetimeformat')
def datetimeformat(value, format):
    d_obj = datetime.datetime.strptime(value, '%Y-%m-%d')
    return d_obj.strftime(format)

@app.template_filter('timestampformat')
def timestampformat(value, format):
    d_obj = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S') # This is where the error occurs.
    current_date = datetime.date.today()
    timestamp_date = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S').date()
    if timestamp_date == current_date:
        timestamp_time = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S').time()
        delta = datetime.timedelta(hours=5, minutes=30)
        timestamp_local = ((datetime.datetime.combine(datetime.date(1,1,1),timestamp_time) + delta).time())
        return "Today at {}".format(timestamp_local.strftime('%I:%M %p'))
    else:
        return d_obj.strftime(format)

template.html (registration['timestamp'] = 2019-09-13 13:29:47


...
        <td>{{ registration['timestamp'] }}</td>
        {% if 'Today' in registration['timestamp']|timestampformat('%B %d, %Y %A \n %I:%M %p') %}
            <td><span class="timestamp today">{{ registration['timestamp']|timestampformat('%B %d, %Y, %A, %I:%M %p') }}</span></td>
        {% else %}
            <td><span class="timestamp">{{ registration['timestamp']|timestampformat('%B %d, %Y, %A, %I:%M %p') }}</span></td>
        {% endif %}

...

我真的很困惑发生了什么事。

1 个答案:

答案 0 :(得分:0)

从python文档中,出于效率原因,每个会话仅加载一次模块。只需重新启动解释器(或本例中的服务器),即可解决此问题。