Python Django:TypeError:object()不接受任何参数
我正在尝试创建聊天应用程序,但遇到此错误。我真的不确定是什么导致了这个错误。 我已经检查了发生错误的文件(asgirf\compatibilty.py),但似乎仍然无法找到导致错误的线索Python Django:TypeError:object()不接受任何参数,python,django,scope,typeerror,Python,Django,Scope,Typeerror,我正在尝试创建聊天应用程序,但遇到此错误。我真的不确定是什么导致了这个错误。 我已经检查了发生错误的文件(asgirf\compatibilty.py),但似乎仍然无法找到导致错误的线索 Exception inside application: object() takes no parameters Traceback (most recent call last): File "C:\Users\aysha\AppData\Local\Programs\Python\Pyth
Exception inside application: object() takes no parameters
Traceback (most recent call last):
File "C:\Users\aysha\AppData\Local\Programs\Python\Python36\lib\site-packages\channels\staticfiles.py", line 44, in __call__
return await self.application(scope, receive, send)
File "C:\Users\aysha\AppData\Local\Programs\Python\Python36\lib\site-packages\channels\routing.py", line 71, in __call__
return await application(scope, receive, send)
File "C:\Users\aysha\AppData\Local\Programs\Python\Python36\lib\site-packages\channels\sessions.py", line 47, in __call__
return await self.inner(dict(scope, cookies=cookies), receive, send)
File "C:\Users\aysha\AppData\Local\Programs\Python\Python36\lib\site-packages\channels\sessions.py", line 254, in __call__
return await self.inner(wrapper.scope, receive, wrapper.send)
File "C:\Users\aysha\AppData\Local\Programs\Python\Python36\lib\site-packages\channels\auth.py", line 181, in __call__
return await super().__call__(scope, receive, send)
File "C:\Users\aysha\AppData\Local\Programs\Python\Python36\lib\site-packages\channels\middleware.py", line 26, in __call__
return await self.inner(scope, receive, send)
File "C:\Users\aysha\AppData\Local\Programs\Python\Python36\lib\site-packages\channels\routing.py", line 160, in __call__
send,
File "C:\Users\aysha\AppData\Local\Programs\Python\Python36\lib\site-packages\asgiref\compatibility.py", line 33, in new_application
instance = application(scope)
TypeError: object() takes no parameters
我的asgi.py:
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
chat.routing.websocket_urlpatterns
)
),
})
和routing.py:
from chat.consumers import EchoConsumer,ChatConsumer
websocket_urlpatterns = [
re_path(r'ws/chat/(?P<username>\w+)/$', ChatConsumer),
re_path(r'ws/chat/', EchoConsumer),
]
我想在调试这方面得到一些帮助。谢谢大家!
class ChatConsumer(SyncConsumer):
def websocket_connect(self, event):
me = self.scope['user']
other_username = self.scope['url_route']['kwargs']['username']
other_user = User.objects.get(username=other_username)
self.thread_obj = Thread.objects.get_or_create_personal_thread(me, other_user)
self.room_name = f'presonal_thread_{self.thread_obj.id}'
async_to_sync(self.channel_layer.group_add)(self.room_name, self.channel_name)
self.send({
'type': 'websocket.accept'
})
print(f'[{self.channel_name}] - You are connected')
def websocket_receive(self, event):
print(f'[{self.channel_name}] - Recieved message - {event["text"]}')
msg = json.dumps({
'text': event.get('text'),
'username': self.scope['user'].username
})
self.store_message(event.get('text'))
async_to_sync(self.channel_layer.group_send)(
self.room_name,
{
'type': 'websocket.message',
'text': msg
}
)
def websocket_message(self, event):
print(f'[{self.channel_name}] - Message sent - {event["text"]}')
self.send({
'type': 'websocket.send',
'text': event.get('text')
})
def websocket_disconnect(self, event):
print(f'[{self.channel_name}] - Disonnected')
async_to_sync(self.channel_layer.group_discard)(self.room_name, self.channel_name)
def store_message(self, text):
Message.objects.create(
thread = self.thread_obj,
sender = self.scope['user'],
text = text
)
class EchoConsumer(SyncConsumer):
def websocket_connect(self, event):
self.room_name = 'broadcast'
self.send({
'type': 'websocket.accept'
})
async_to_sync(self.channel_layer.group_add)(self.room_name, self.channel_name)
print(f'[{self.channel_name}] - You are connected')
def websocket_receive(self, event):
print(f'[{self.channel_name}] - Recieved message - {event["text"]}')
async_to_sync(self.channel_layer.group_send)(
self.room_name,
{
'type': 'websocket.message',
'text': event.get('text')
}
)
def websocket_message(self, event):
print(f'[{self.channel_name}] - Message sent - {event["text"]}')
self.send({
'type': 'websocket.send',
'text': event.get('text')
})
def websocket_disconnect(self, event):
print(f'[{self.channel_name}] - Disconnected')
async_to_sync(self.channel_layer.group_discard)(self.room_name, self.channel_name)
def store_message(self, text):
Message.objects.create(
thread = self.thread_obj,
sender = self.scope['user'],
text = text
)