debug sanic with pycharm when
, if we want to keep
debug=True we also have to explicitly mention auto_reload=False
Is this a bug?
I'm having an issue with request.ctx and middleware.
@app.middleware('request') async def _prepare(request): request.ctx.test = '1234' @app.middleware('response') async def _cleanup(request, response): test = request.ctx.test #test = getattr(request.ctx, 'test')
I'm getting this error
AttributeError: 'types.SimpleNamespace' object has no attribute 'test'
I guess my question is if this is the same ctx as in _prepare, or if I have to do anything extra to get _cleanup the same info
I've made this small example to help recreate the issue.
from sanic import Sanic from sanic.response import HTTPResponse app = Sanic("middleware_test") @app.middleware("request") async def _prepare(request): request.ctx.test = 'ASDF' @app.middleware("response") async def _cleanup(request, response): test = request.ctx.test @app.route("/") async def test(request): test = request.ctx.test return HTTPResponse("Got Test: %s" % test, content_type='text/html') if __name__ == "__main__": app.run(host="0.0.0.0", port=8888)
If you run the code, you'll notice the handler renders the response just fine, but if you look at the console output you'll notice the traceback:
[2020-07-04 21:45:19 +0000]  [ERROR] Exception occurred in one of response middleware handlers Traceback (most recent call last): File "<path>/env/lib/python3.6/site-packages/sanic/app.py", line 977, in handle_request request, response, request_name=name File "<path>/env/lib/python3.6/site-packages/sanic/app.py", line 1281, in _run_response_middleware _response = await _response File "mwtest.py", line 12, in _cleanup test = request.ctx.test AttributeError: 'types.SimpleNamespace' object has no attribute 'test'
Got a quick question about csrf tokens for sanic. With the 20.3 release, looks like sanic-wtf no longer works and there is a pull request that hasn't been merged. pyx/sanic-wtf#17
The developer doesn't seem to be active anymore, are there any alternatives to this library?
@ahopkins Awesome I hope your talk went well, if there is a video of it online, I'd be happy to give it a watch. I ended up throwing the functions into a class and it worked great.
One minor thing I noticed:
decoded = cipher.decrypt(token) ->
decoded = cipher.decrypt(token, ttl)
Will help make the ttl active :)