python - ImportError: When Trying to Import Custom File - Django JWT -


i'm using django-rest-framework-jwt generate token security, need user information token when logs in. way have seen create custom function override default functionality. i'm new django i'm still trying figure out how things work, have tried after reading this article. have tried many ways working, seems best approach.

problem have when use current setup get:

importerror: not import 'custom_jwt.jwt_response_payload_handler' api setting 'jwt_payload_handler'. importerror: no module named custom_jwt. 

1 - after creating custom_jwt.py best practices on put it? if there none, suggestions on where?

2- how gain access functions in custom_jwt.py in settings.py?

settings.py

jwt_auth = {      'jwt_payload_handler':     'custom_jwt.jwt_response_payload_handler',      'jwt_response_payload_handler':     'custom_jwt.jwt_payload_handler', } 

custom_jwt.py

from datetime import datetime calendar import timegm rest_framework_jwt.settings import api_settings   def jwt_payload_handler(user):     """     custom payload handler      token encrypts dictionary returned function, , can decoded rest_framework_jwt.utils.jwt_decode_handler     """     return {         'user_id': user.pk,         'email': user.email,         'is_superuser': user.is_superuser,         'exp': datetime.utcnow() + api_settings.jwt_expiration_delta,         'orig_iat': timegm(             datetime.utcnow().utctimetuple()         )     }   def jwt_response_payload_handler(token, user=none, request=none):     """      custom response payload handler.     function controlls custom payload after login or token refresh. data returned through web api.     """     return {         'token': token,         'user': {              'email': user.email,         }     } 

project structure

project     account         __init__.py         admin.py         apps.py         managers.py         models.py         serializers.py         tests.py         views.py     core         __init__.py         settings.py         custom_jwt.py         urls.py         wsgi.py         db.sqlite3 

python version

python 2.7.10 

i ended moving file account @ point resided in app(module). looking @ site-packages > rest_framework_jwt > utils.py learned called file utils.py feel more of standard, renamed custom_jwt.py utils.py.

settings.py

jwt_auth = {     """     found did not need change jwt_payload_handler     """     'jwt_payload_handler':     'rest_framework_jwt.utils.jwt_payload_handler',      'jwt_response_payload_handler':     'account.utils.jwt_response_payload_handler', } 

utils.py

# created custom user need import serializer .serializers import userserializer   def jwt_response_payload_handler(token, user=none, request=none):     """     custom response payload handler.     function controls custom payload after login or token refresh. data returned through web api.     """      return {         'token': token,         'user': userserializer(user, context={'request': request}).data     } 

desired response

{"token":"....", "user":{"id":1, "email": "test@testing.com", "first": "test", "last": "guy"}} 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -