ImportError:尝试导入自定义文件时 - Django JWT

时间:2017-08-14 18:16:28

标签: python django python-2.7 django-views django-rest-framework

我正在使用 django-rest-framework-jwt 来生成安全性令牌,但是当有人登录时我需要使用令牌获取用户信息。这是我见过的唯一方式这样做是为了创建一个将覆盖默认功能的自定义函数。我对Django很新,所以我还在努力弄清楚它是如何工作的,这是我在阅读THIS文章后尝试过的。我已经尝试了很多方法来实现这一点,但这似乎是最好的方法。

当我使用当前设置时遇到问题:

  

ImportError:无法为API设置'JWT_PAYLOAD_HANDLER'导入'custom_jwt.jwt_response_payload_handler'。 ImportError:没有名为custom_jwt的模块。

1 - 创建custom_jwt.py之后,最佳做法是什么?如果没有,有什么建议吗?

2-如何访问custom_jwt.pysettings.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
from calendar import timegm
from rest_framework_jwt.settings import api_settings


def jwt_payload_handler(user):
    """
    Custom payload handler 
    Token encrypts the dictionary returned by this function, and can be decoded by 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.
    This function controlls the custom payload after login or token refresh. This data is returned through the web API.
    """
    return {
        'token': token,
        'user': {
             'email': user.email,
        }
    }

项目结构

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版本

Python 2.7.10

1 个答案:

答案 0 :(得分:1)

我最终将文件移动到class Questionnaire extends React.Component { constructor (props) { super(props); this.state = { selectedSection: 0 }; this.selectSection = this.selectSection.bind(this) } ,此时该文件位于app(模块)中。通过查看account,我了解到他们调用的文件site-packages > rest_framework_jwt > utils.py我认为更符合标准,因此我将utils.py重命名为custom_jwt.py

settings.py

utils.py

utils.py

JWT_AUTH = {
    """
    I found I did not need to change JWT_PAYLOAD_HANDLER
    """
    'JWT_PAYLOAD_HANDLER':
    'rest_framework_jwt.utils.jwt_payload_handler',

    'JWT_RESPONSE_PAYLOAD_HANDLER':
    'account.utils.jwt_response_payload_handler',
}

期望的回复

# I created a custom User so I need to import the serializer
from .serializers import UserSerializer


def jwt_response_payload_handler(token, user=None, request=None):
    """
    Custom response payload handler.
    This function controls the custom payload after login or token refresh. This data is returned through the web API.
    """

    return {
        'token': token,
        'user': UserSerializer(user, context={'request': request}).data
    }