django-critical
django-critical copied to clipboard
middleware: TypeError: object() takes no parameters
When I enable
critical.middleware.CriticalCssMiddleware
it breaks the application with following error.
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10c725598>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 140, in inner_run
handler = self.get_handler(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
handler = super().get_handler(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 65, in get_handler
return get_internal_wsgi_application()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 44, in get_internal_wsgi_application
return import_string(app_path)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/module_loading.py", line 17, in import_string
module = import_module(module_path)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/03000177/Sites/COCO/cocomotor/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/wsgi.py", line 140, in __init__
self.load_middleware()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", line 39, in load_middleware
mw_instance = middleware(handler)
TypeError: object() takes no parameters
As suggested in other places I changed following
class CriticalCssMiddleware(object):
to
from django.utils.deprecation import MiddlewareMixin
class CriticalCssMiddleware(MiddlewareMixin):
Django app works fine but when I load the page it breaks with following error:
Internal Server Error: /motor-insurance/car/
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/deprecation.py", line 97, in __call__
response = self.process_response(request, response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/critical/middleware.py", line 35, in process_response
match = CRITICAL_CSS_RE.search(content)
TypeError: cannot use a string pattern on a bytes-like object
Request Method: | GET
-- | --
http://127.0.0.1:8000/motor-insurance/car/
2.0.3
TypeError
cannot use a string pattern on a bytes-like object
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/critical/middleware.py in process_response, line 35
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
3.6.7
['/Users/03000177/Sites/COCO', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages', '/Users/03000177/Sites/COCO']
Tue, 13 Nov 2018 11:35:55 +0530
Thanks for debugging this! Happy to merge the MiddlewareMixin if you send a Pull Request.
About the "string pattern on a bytes-like object" this is probably a Python 3 issue. When I first wrote this experiment I was using Python 2 where strings are bytes by default. It will probably take some work make this work with Python 3, I haven't maintained this project. Happy to merge pull requests though!
Thanks for debugging this! it's a difficult question for me, but was