在将python2项目移植到python3

时间:2019-09-16 10:04:27

标签: django python-3.x

在将带有Django 1.1.11的python2项目移植到python 3.7和Django 2.2.5时,我遇到了依赖项错误。

我在python2中使用Django-mobileesp,当我将代码更新为python3时,它为UserAgent提供了中间件错误。

  

线程django-main-thread中的异常:       追溯(最近一次通话):         _bootstrap_inner中的文件“ /usr/lib/python3.7/threading.py”,第917行           self.run()         运行中的文件“ /usr/lib/python3.7/threading.py”,行865           self._target(* self._args,** self._kwargs)         包装中的文件“ /home/jarvis/Desktop/law3/lib/python3.7/site-packages/django/utils/autoreload.py”,第54行           fn(* args,** kwargs)         在inner_run中,文件“ /home/jarvis/Desktop/law3/lib/python3.7/site-packages/django/core/management/commands/runserver.py”,第137行           处理程序= self.get_handler(* args,** options)         get_handler中的文件“ /home/jarvis/Desktop/law3/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/runserver.py”,第27行           处理程序= super()。get_handler(* args,** options)         在get_handler中,文件“ /home/jarvis/Desktop/law3/lib/python3.7/site-packages/django/core/management/commands/runserver.py”,第64行           返回get_internal_wsgi_application()         在get_internal_wsgi_application中的第45行,文件“ /home/jarvis/Desktop/law3/lib/python3.7/site-packages/django/core/servers/basehttp.py”           返回import_string(app_path)         文件“ /home/jarvis/Desktop/law3/lib/python3.7/site-packages/django/utils/module_loading.py”,第17行,在import_string中           模块= import_module(模块路径)         在import_module中的文件“ /home/jarvis/Desktop/law3/lib/python3.7/importlib/init.py”,第127行           返回_bootstrap._gcd_import(name [level:],包,级别)         _gcd_import中的文件“”,第1006行         _find_and_load中的文件“”,第983行         _find_and_load_unlocked中的文件“”,行967         _load_unlocked中的文件“”,第677行         exec_module中的文件“”,第728行         _call_with_frames_removed中的文件“”,第219行         在第5行的“ /home/jarvis/Desktop/law3/src/lawersify/wsgi.py”文件中           应用程序= get_wsgi_application()         在get_wsgi_application中的第13行,文件“ /home/jarvis/Desktop/law3/lib/python3.7/site-packages/django/core/wsgi.py”           返回WSGIHandler()          init 中的文件“ /home/jarvis/Desktop/law3/lib/python3.7/site-packages/django/core/handlers/wsgi.py”,第135行           self.load_middleware()         在load_middleware的第37行,文件“ /home/jarvis/Desktop/law3/lib/python3.7/site-packages/django/core/handlers/base.py”           mw_instance =中间件(处理程序)       TypeError:UserAgentDetectionMiddleware()不带任何参数

settings.py

MIDDLEWARE =  [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django_mobileesp.middleware.UserAgentDetectionMiddleware'
]


#middleware.py#

from django.conf import settings
from django.utils.functional import SimpleLazyObject

DETECT_USER_AGENTS = getattr(settings, 'DETECT_USER_AGENTS', {})

def lazy_detection(request, key):
    detector = DETECT_USER_AGENTS[key]
    return SimpleLazyObject( lambda: detector(request) )


class UserAgentDetectionMiddleware(object):
    """
    Middleware to detect request's user agent
    """
    def process_request(self, request):
        for each in DETECT_USER_AGENTS:
            setattr( request, each, lazy_detection(request, each) )

1 个答案:

答案 0 :(得分:0)

这不是Python版本问题,而是Django版本问题。旧的(Django 1.10之前的版本)中间件处理方式以及MIDDLEWARE_CLASSES设置已在Django 2.0中删除。有关如何使中间件与新的中间件结构兼容的信息,请参见the docs