WebSocket in Django

I started working with websockets for the first time in my life and this error occurred. I have already used the tips to install the websocket-client, remove it, and reinstall it again. It didn't help. Please tell me what the error is, how to fix it.

Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Python\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Python\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Python\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Python\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
    self.check(display_num_errors=True)
  File "C:\Python\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = checks.run_checks(
  File "C:\Python\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Python\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Python\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Python\lib\site-packages\django\urls\resolvers.py", line 408, in check
    for pattern in self.url_patterns:
  File "C:\Python\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Python\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Python\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Александра\PycharmProjects\smarthome\smarthome\urls.py", line 27, in <module>
    websocket("ws/", views.websocket_view),
AttributeError: module 'firstapp.views' has no attribute 'websocket_view'

My code:

Mysite/websocket/urls.py

from django.urls import path
websocket = path

Mysite/urls.py

from django.contrib import admin
from django.urls import path
from firstapp import views
from websocket.urls import websocket
from django.contrib.auth.views import LoginView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index),
    path('finfo/', views.full_info),
    path('login/', LoginView.as_view(), name='login'),
    websocket("ws/", views.websocket_view),
]

Mysite/settings.py

INSTALLED_APPS = [
    'websocket',
    'users',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'firstapp',
]

Mysite/asgi.py

import os
from websocket.middleware import websockets
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'smarthome.settings')

application = get_asgi_application()
application = websockets(application)

Mysite/websocket/middleware.py

from django.urls import resolve
from .connection import WebSocket

def websockets(app):
    async def asgi(scope, receive, send):
        if scope["type"] == "websocket":
            match = resolve(scope["raw_path"])
            await match.func(WebSocket(scope, receive, send),*match.args,**match.kwargs)
            return
    await app(scope, receive, send)
    return asgi

Mysite/websocket/views.py

from django.shortcuts import render

Mysite/users.py/views.py

from django.views.generic.base import TemplateView

class IndexView(TemplateView):
    template_name = "index.html"

async def websocket_view(socket):
    await socket.accept()
    await socket.send_text('hello')
    await socket.close()
Author: Ольга, 2020-09-17

1 answers

Judging by the logs, it can't find views. websocket_view, either the function is not exported, or the nesting is incorrect, or there is a syntax error inside. You may need to add a stumble async def websocket_view(socket):

 0
Author: Arturas Lapinskas, 2020-09-21 05:54:23