首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于路径和自定义装饰器的Python3 Django问题

关于路径和自定义装饰器的Python3 Django问题
EN

Stack Overflow用户
提问于 2019-11-18 04:37:17
回答 1查看 170关注 0票数 2

对于第一次使用它的Django来说,这是非常新鲜的。Python不是我在web上下文中的强项--我的定制装饰器有问题,它会从所有请求中解码jwt,而不是真正确定它在寻找什么。

错误消息可以在最下面找到。下面是我的文件,我只是尝试使用verify_token()作为装饰器,在接收HTTP请求时包含自定义令牌检查器(请不要推荐simple-jwt或django会话令牌),而不是我们想要做的事情,因为我们正在处理来自其他DBS的遗留表,并且不需要任何抽象)

decorators.py

代码语言:javascript
复制
from django.core.exceptions import PermissionDenied
import jwt


def verify_token(function):
    def wrap(request, *args, **kwargs):
        if request:
            print('========='.format(request))
            jwt.decode(request, 'secret', algorithms=['HS256'])

        else:
            raise PermissionDenied
        wrap.__doc__ = function.__doc__
        wrap.__name__ = function.__name__
        return wrap

views.py

代码语言:javascript
复制
from .User_Predictions.PredictionService import PredicitonController
from django.http import JsonResponse
from rest_framework.views import APIView
from .decorators import verify_token


class UserPredictions(APIView):
    @verify_token
    def generate_full_report(request):
        _predction = PredicitonController()
        results = _predction.compile_complete_predition()
        return JsonResponse(results, safe=False)

urls.py

代码语言:javascript
复制
from django.urls import path
from .views import UserPredictions


urlpatterns = [
    path('compilePredictions/', UserPredictions.generate_full_report, name='generate_full_report')
]

代码语言:javascript
复制
├── Analytics
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── settings.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── wsgi.cpython-37.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── authentication
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── biometrics
│   ├── admin.py
│   ├── apps.py
│   ├── bodyfatPrediciton.py
│   ├── Config.py
│   ├── __init__.py
│   ├── models.py
│   ├── templates
│   │   ├── bodyfat.html
│   │   ├── github.html
│   │   └── upload_bodyfat.html
│   ├── tests.py
│   ├── TorsoDimensions.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── dump.rdb
├── extraction
│   ├── admin.py
│   ├── apps.py
│   ├── ExtractionQueryService.py
│   ├── ExtractionServices.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-37.pyc
│   │   ├── ExtractionQueryService.cpython-37.pyc
│   │   ├── ExtractionServices.cpython-37.pyc
│   │   ├── __init__.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── loginServices
│   ├── admin.py
│   ├── apps.py
│   ├── Authentication.py
│   ├── decorators.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-37.pyc
│   │   ├── Authentication.cpython-37.pyc
│   │   ├── __init__.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
├── models.py
├── nutrition
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_auto_20191007_0323.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       ├── 0001_initial.cpython-37.pyc
│   │       ├── 0002_auto_20191007_0323.cpython-37.pyc
│   │       └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── PredictFood.py
│   ├── __pycache__
│   │   ├── admin.cpython-37.pyc
│   │   ├── __init__.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── PredictFood.cpython-37.pyc
│   │   ├── serializers.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── serializers.py
│   ├── TempImages
│   ├── templates
│   │   └── food.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── output
**├── predictions**
│   ├── admin.py
│   ├── apps.py
*│   ├── decorators.py*
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-37.pyc
│   │   ├── decorators.cpython-37.pyc
│   │   ├── __init__.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── tests.py
*│   ├── urls.py*
│   ├── User_Predictions
*│   └── views.py*
├── README.txt
└── TempImages

错误信息

代码语言:javascript
复制
/Development/Backends_HealthApp/mainBackend/ModelBackend/Analytics/predictions/urls.py", line 6, in <module>
    path('compilePredictions/', UserPredictions.generate_full_report, name='generate_full_report')
  File "/home/travjav/.local/lib/python3.7/site-packages/django/urls/conf.py", line 73, in _path
    raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().

不太清楚这是怎么回事--我可以用其他没有问题的非定制装潢师。

EN

回答 1

Stack Overflow用户

发布于 2019-11-20 04:04:19

装饰师就是这样被创造出来的。

我更改了函数名,但您可以看到不同之处。

代码语言:javascript
复制
from django.core.exceptions import PermissionDenied
import jwt
from functools import wraps
from django.http import HttpResponseRedirect, HttpResponse

def protected_endpoint(function):
    @wraps(function)
    def wrap(request, *args, **kwargs):
        auth = request.headers.get('Authorization').split()[1]
        if auth is None:
            return PermissionDenied
        elif auth is not None:
            try:
                session_token = jwt.decode(auth, '', 'utf-8')
            except jwt.DecodeError:
                return HttpResponse('Invalid Token')
        if session_token:
            return function(request, *args, **kwargs)
        else:
            return HttpResponseRedirect('/')
    return wrap
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58908038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档