【问题标题】:ImportError: When Trying to Import Custom File - Django JWTImportError:尝试导入自定义文件时 - Django JWT
【发布时间】:2018-01-22 15:36:21
【问题描述】:

我正在使用 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.py 中的函数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
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

【问题讨论】:

  • 什么python版本?
  • 忘记包含了 - Python 2.7.10
  • 我查看了 site-packages > rest_framework_jwt > utils.py 以了解它是如何工作的。我注意到我的custom_jwt.py 可能在错误的应用程序中,所以我将其移至account 并更改了settings.py 并且它起作用了......一些什么。一个我得到它的工作我会发布我为未来的窥视所做的事情。

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


【解决方案1】:

我最终将我的文件移动到account,当时它位于一个应用程序(模块)中。通过查看site-packages > rest_framework_jwt > utils.py,我了解到他们将他们的文件称为utils.py,我觉得这更像是一个标准,所以我将custom_jwt.py 重命名为utils.py

settings.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',
}

utils.py

# 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
    }

期望的响应

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 2018-04-11
    • 1970-01-01
    • 2014-09-06
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多