1

确实是我正在处理的一个非常奇怪的错误。奇怪的是,当我在 pyCharm 的 localhost 上运行同一个项目时,它运行良好,但是,当我将所有文件上传到 EC2 服务器时。我收到以下错误。

该项目运行良好,因此我能够托管应用程序,但是当我发送发布请求时,我收到 HTTP ERROR CODE 500 作为响应并在控制台中出现以下错误。

如果有人可以帮助我纠正这个问题,我将不胜感激

    Traceback (most recent call last):
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 2464, in __cal                                                                                                l__
    return self.wsgi_app(environ, start_response)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 2450, in wsgi_                                                                                                app
    response = self.handle_exception(e)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask_restful/__init__.py", line 2                                                                                                72, in error_router
    return original_handler(e)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 1867, in handl                                                                                                e_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/_compat.py", line 38, in rer                                                                                                aise
    raise value.with_traceback(tb)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_                                                                                                app
    response = self.full_dispatch_request()
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 1952, in full_                                                                                                dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask_restful/__init__.py", line 2                                                                                                72, in error_router
    return original_handler(e)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 1821, in handl                                                                                                e_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/_compat.py", line 38, in rer                                                                                                aise
    raise value.with_traceback(tb)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 1950, in full_                                                                                                dispatch_request
    rv = self.dispatch_request()
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 1936, in dispa                                                                                                tch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask_restful/__init__.py", line 4                                                                                                68, in wrapper
    resp = resource(*args, **kwargs)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask_restful/__init__.py", line 5                                                                                                83, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/home/ubuntu/yAPI/resources/r_user.py", line 50, in post
    user = user_schema.load(request.get_json())
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/marshmallow_sqlalchemy/schema/load                                                                                                _instance_mixin.py", line 89, in load
    raise ValueError("Deserialization requires a session")
ValueError: Deserialization requires a session

在我的应用程序中,这是错误开始的地方。

class UserRegister(Resource):

    @classmethod
    def post(cls):
        #        the 'load' function in marshmallow will use the data to create usermodel object
        user = user_schema.load(request.get_json())
4

1 回答 1

2

正如评论中所讨论的,这是解决方案,因此对其他人有帮助:

您需要定义一个会话并将其传递给 load 函数,如下所示:

from sqlalchemy import engine #thanks to your comment
from sqlalchemy.orm import scoped_session, sessionmaker



class UserRegister(Resource):

    @classmethod
    def post(cls):
        #        the 'load' function in marshmallow will use the data to create usermodel object
        sess = scoped_session(sessionmaker(bind=engine))
        user = user_schema.load(request.get_json(), sess)
于 2020-04-27T14:38:20.307 回答