J
J
jinjim2018-07-22 17:53:59
JSON
jinjim, 2018-07-22 17:53:59

Error in Flask: TypeError: b'' is not JSON serializable. What's wrong?

When using the flask-wtf extension, it gives an error:

TypeError: b'be3d65bc5d4d3c9de2c8fcea23a32750e83e5ba2' is not JSON serializable

I simplified the code as much as possible:
start.py
from flask import render_template, Flask
from forms import LoginForm

app = Flask(__name__)
app.config['SECRET_KEY'] = 'you-will-never-guess'

@app.route('/')
def login():
    form = LoginForm()
#    return render_template('login.html', form=form)

if __name__ == '__main__':
  app.run(debug=True)


forms.py
from flask_wtf import FlaskForm

class LoginForm(FlaskForm):
    pass


Stack:
Traceback (most recent call last):
File "/var/www/learn_flask/one/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/var/www/learn_flask/one/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/var/www/learn_flask/ one/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/var/www/learn_flask/one/lib/python3.5/site -packages/flask/_compat.py", line 35, in reraise
raise value
File "/var/www/learn_flask/one/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/var/www/learn_flask/one/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File " /var/www/learn_flask/one/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/var/www/learn_flask/one/ lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/var/www/learn_flask/one/lib/python3.5/site-packages/flask/app.py ", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/var/www/learn_flask/one/lib/python3.5/site-packages/flask/app.py", line 1799,in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/www/learn_flask/test/start.py", line 9, in login
form = LoginForm()
File "/var/www/learn_flask /one/lib/python3.5/site-packages/wtforms/form.py", line 212, in __call__
return type.__call__(cls, *args, **kwargs)
File "/var/www/learn_flask/one/ lib/python3.5/site-packages/flask_wtf/form.py", line 88, in __init__
super(FlaskForm, self).__init__(formdata=formdata, **kwargs)
File "/var/www/learn_flask/one/ lib/python3.5/site-packages/wtforms/form.py", line 278, in __init__
self.process(formdata, obj, data=data, **kwargs)
File "/var/www/learn_flask/one/lib /python3.5/site-packages/wtforms/form.py",line 132, in process
field.process(formdata)
File "/var/www/learn_flask/one/lib/python3.5/site-packages/wtforms/csrf/core.py", line 43, in process
self.current_token = self.csrf_impl.generate_csrf_token (self)
File "/var/www/learn_flask/one/lib/python3.5/site-packages/flask_wtf/csrf.py", line 134, in generate_csrf_token
token_key=self.meta.csrf_field_name
File "/var/www/ learn_flask/one/lib/python3.5/site-packages/flask_wtf/csrf.py", line 47, in generate_csrf
setattr(g, field_name, s.dumps(session[field_name]))
File "/var/www/learn_flask /one/lib/python3.5/site-packages/itsdangerous.py", line 565, in dumps
payload = want_bytes(self.dump_payload(obj))
File "/var/www/learn_flask/one/lib/python3.5/site-packages/itsdangerous.py", line 847, in dump_payload
json = super(URLSafeSerializerMixin, self).dump_payload(obj)
File "/var/www /learn_flask/one/lib/python3.5/site-packages/itsdangerous.py", line 550, in dump_payload
return want_bytes(self.serializer.dumps(obj))
File "/var/www/learn_flask/one/lib/ python3.5/site-packages/itsdangerous.py", line 51, in dumps
return json.dumps(obj, separators=(',', ':'))
File "/usr/lib/python3.5/json/ __init__.py", line 237, in dumps
**kw).encode(obj)
File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.5/json/encoder.py", line 179 , in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'be3d65bc5d4d3c9de2c8fcea23a32750e83e5ba2' is not JSON serializable


What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2018-07-22
@dimonchik2013

decode it into a string from the binary

V
Vladimir, 2018-07-23
@vintello

json sterilizes a dictionary but not a string

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question