Python 我想用django soet创建一个自定义中间件
我想在我的项目中创建自定义中间件。但我遇到了这样的错误:Python 我想用django soet创建一个自定义中间件,python,django,python-3.x,python-2.7,django-middleware,Python,Django,Python 3.x,Python 2.7,Django Middleware,我想在我的项目中创建自定义中间件。但我遇到了这样的错误: [ django.core.exceptions.ImproperlyConfigured: WSGI application 'custommiddle.wsgi.application' could not be loaded; Error importing module. ] 我想问题可能是我的python版本,我使用的是版本3.7 以下是我的文件夹结构: custommiddle/ # project name __i
[ django.core.exceptions.ImproperlyConfigured: WSGI application 'custommiddle.wsgi.application' could not be loaded; Error importing module. ]
我想问题可能是我的python版本,我使用的是版本3.7
以下是我的文件夹结构:
custommiddle/ # project name
__init__.py
settings.py
urls.py
wsgi.py
cmiddle/ # app name
middle/ # directory
__init__.py
middleware.py
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
中间件.py
from django.conf import settings
class StackOverflowMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
return response
def process_exception(self, request, exception):
if settings.DEBUG:
print (exception.__class__.__name__)
print (exception.message)
return None
INSTALLED_APPS = [ 'soet' ]
MIDDLEWARE_CLASSES = [ 'soet.middleware.StackOverFlowMiddleware' ]
import os
BASE_DIR = ...
SECRET_KEY = ...
DEBUG = ...
ALLOWED_HOSTS = ...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'soet', # your apps
]
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',
'soet.middleware.StackOverFlowMiddleware' # your_middleware
]
.
.
.
设置.py
from django.conf import settings
class StackOverflowMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
return response
def process_exception(self, request, exception):
if settings.DEBUG:
print (exception.__class__.__name__)
print (exception.message)
return None
INSTALLED_APPS = [ 'soet' ]
MIDDLEWARE_CLASSES = [ 'soet.middleware.StackOverFlowMiddleware' ]
import os
BASE_DIR = ...
SECRET_KEY = ...
DEBUG = ...
ALLOWED_HOSTS = ...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'soet', # your apps
]
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',
'soet.middleware.StackOverFlowMiddleware' # your_middleware
]
.
.
.
我假设您已正确编辑了设置.py。因为中间件部分似乎是正确的。您能否确认您正确编辑了settings.py,如下所示 设置.py
from django.conf import settings
class StackOverflowMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
return response
def process_exception(self, request, exception):
if settings.DEBUG:
print (exception.__class__.__name__)
print (exception.message)
return None
INSTALLED_APPS = [ 'soet' ]
MIDDLEWARE_CLASSES = [ 'soet.middleware.StackOverFlowMiddleware' ]
import os
BASE_DIR = ...
SECRET_KEY = ...
DEBUG = ...
ALLOWED_HOSTS = ...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'soet', # your apps
]
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',
'soet.middleware.StackOverFlowMiddleware' # your_middleware
]
.
.
.
我假设您已正确编辑了设置.py。因为中间件部分似乎是正确的。您能否确认您正确编辑了settings.py,如下所示 设置.py
from django.conf import settings
class StackOverflowMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
return response
def process_exception(self, request, exception):
if settings.DEBUG:
print (exception.__class__.__name__)
print (exception.message)
return None
INSTALLED_APPS = [ 'soet' ]
MIDDLEWARE_CLASSES = [ 'soet.middleware.StackOverFlowMiddleware' ]
import os
BASE_DIR = ...
SECRET_KEY = ...
DEBUG = ...
ALLOWED_HOSTS = ...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'soet', # your apps
]
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',
'soet.middleware.StackOverFlowMiddleware' # your_middleware
]
.
.
.
soet正在使用python2语法。在Python 3中,print语句已替换为print()函数 看看下面的代码库 您可以将其更改为python3语法(基本上只查看print语句)
访问django文档创建中间件,网址为:soet正在使用python2语法。在Python 3中,print语句已替换为print()函数 看看下面的代码库 您可以将其更改为python3语法(基本上只查看print语句)
访问django文档以创建中间件,网址为:I在soet\middleware.py中解决了print语句。但是它会抛出同样的错误你能显示出你的错误的全部痕迹吗。也许您的应用程序中存在其他问题,只需卸载一次中间件即可检查。我在soet\middleware.py中解决了print语句。但是它会抛出同样的错误你能显示出你的错误的全部痕迹吗。您的应用程序中可能存在其他问题,只需卸载一次中间件即可进行检查。请在settings.py文件中正确配置所有内容。但它会抛出相同的错误,即在settings.py文件中正确配置的所有内容。但它抛出了同样的错误