Flask + sqlalchemy - NameError:全局名称'字符串未定义

时间:2014-10-03 20:45:56

标签: python postgresql flask sqlalchemy

无法弄清楚为什么有时会抛出此异常 - 可能是五次尝试中的一次。

我有一个简单的API,它接受一个文件,存储它,并在postgres数据库中记录一些元数据。

这是日志:

==> /var/log/apache2/error.log <==
[Fri Oct 03 20:32:02.016347 2014] [:error] [pid 28259] ERROR:Exception on /api/v1.0/upload [POST]
[Fri Oct 03 20:32:02.016454 2014] [:error] [pid 28259] Traceback (most recent call last):
[Fri Oct 03 20:32:02.016496 2014] [:error] [pid 28259]   File "/usr/local/lib/python2.7/dist-    packages/flask/app.py", line 1817, in wsgi_app
[Fri Oct 03 20:32:02.016534 2014] [:error] [pid 28259]     response = self.full_dispatch_request()
[Fri Oct 03 20:32:02.016576 2014] [:error] [pid 28259]   File "/usr/local/lib/python2.7/dist- packages/flask/app.py", line 1477, in full_dispatch_request
[Fri Oct 03 20:32:02.016615 2014] [:error] [pid 28259]     rv = self.handle_user_exception(e)
[Fri Oct 03 20:32:02.016652 2014] [:error] [pid 28259]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
[Fri Oct 03 20:32:02.016689 2014] [:error] [pid 28259]     reraise(exc_type, exc_value, tb)
[Fri Oct 03 20:32:02.016725 2014] [:error] [pid 28259]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
[Fri Oct 03 20:32:02.016762 2014] [:error] [pid 28259]     rv = self.dispatch_request()
[Fri Oct 03 20:32:02.016798 2014] [:error] [pid 28259]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
[Fri Oct 03 20:32:02.016831 2014] [:error] [pid 28259]     return self.view_functions[rule.endpoint](**req.view_args)
[Fri Oct 03 20:32:02.016863 2014] [:error] [pid 28259]   File "__init__.py", line 95, in upload
[Fri Oct 03 20:32:02.016895 2014] [:error] [pid 28259]     width=_file_data['Image width'],
[Fri Oct 03 20:32:02.016939 2014] [:error] [pid 28259] NameError: global name 'string' is not defined

在我看来:

from database import session
from models import SomeInfo

db_entry = SomeInfo(name=f.filename,
                    width=_file_data['Image width'],
                    height=_file_data['Image height'],
                    mime_type=_file_data['MIME type'],
                    duration=_file_data['Duration'],
                    owner=user.username,
                    location=os.path.join(config._upload_directory, f.filename))

我的模特:

from sqlalchemy import Column, Integer, String, Sequence, Text, DateTime, ForeignKey
from database import Base, engine

import datetime


class SomeInfo(Base):
    __tablename__ = 'some_info'

    id = Column(Integer, primary_key=True)
    name = Column(String(2048))
    width = Column(String(2048))
    height = Column(String(2048))
    upload_date = Column(DateTime, default=datetime.datetime.today())
    last_modification = Column(DateTime, default=datetime.datetime.today())
    SomeMoreStuff

Base.metadata.create_all(engine)

Sqlalchemy使用postgres作为后端。

就像我说的那样,这只是偶尔会发生,每隔五个帖子左右就会发生一次。

非常感谢任何见解!

85-95行按要求:

text = metadata.exportPlaintext()
_file_data = {}
for item in text[1:]:
    _file_data[item.split(':')[0].strip(' -')] = item.split(':')[1]

_file_data['name'] = f.filename

0 个答案:

没有答案