diff --git a/apps/back/consumers.py b/apps/back/consumers.py index 71cd5cb..724a729 100644 --- a/apps/back/consumers.py +++ b/apps/back/consumers.py @@ -1,11 +1,12 @@ import json -from channels.generic.websocket import AsyncWebsocketConsumer +from channels.generic.websocket import WebsocketConsumer +from django.template.loader import render_to_string -class BlogConsumer(AsyncWebsocketConsumer): +class BlogConsumer(WebsocketConsumer): def connect(self): ''' Cliente se conecta ''' - + print('cooooooooooooonectando') # Recoge el nombre de la sala self.room_name = self.scope["url_route"]["kwargs"]["room_name"] self.room_group_name = "blog_%s" % self.room_name @@ -16,6 +17,17 @@ class BlogConsumer(AsyncWebsocketConsumer): # Informa al cliente del éxito self.accept() + # Send message to WebSocket + self.send( + text_data=json.dumps( + { + "selector": "#articles", + "position": "appendChild", + "html": render_to_string('blog/articles.html', {'pag': 1}) + } + ) + ) + def disconnect(self, close_code): ''' Cliente se desconecta ''' # Leave room group diff --git a/apps/front/templates/blog.html b/apps/front/templates/blog.html deleted file mode 100644 index 2c2273c..0000000 --- a/apps/front/templates/blog.html +++ /dev/null @@ -1,125 +0,0 @@ - - - - - Chat - - - - -
-

Chat con Django

-
- -
- - -
- - - -
- - -
- -
-
-
- - - diff --git a/apps/front/views.py b/apps/front/views.py index b19f6f5..36a63ab 100644 --- a/apps/front/views.py +++ b/apps/front/views.py @@ -1,4 +1,7 @@ from django.shortcuts import render +import uuid -def blog(request): - return render(request, 'blog.html') +def all_articles(request): + return render(request, 'layouts/main.html', { + "CHANNEL": uuid.uuid4().hex[:20].upper() + }) diff --git a/my_demo/settings.py b/my_demo/settings.py index d35a7c9..579fca6 100644 --- a/my_demo/settings.py +++ b/my_demo/settings.py @@ -120,7 +120,7 @@ USE_TZ = True STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') -ASGI_APPLICATION = "asgi.application" +ASGI_APPLICATION = "my_demo.asgi.application" CHANNEL_LAYERS = { "default": { diff --git a/my_demo/urls.py b/my_demo/urls.py index ebd09c8..54da03e 100644 --- a/my_demo/urls.py +++ b/my_demo/urls.py @@ -19,5 +19,5 @@ from apps.front import views urlpatterns = [ path('admin/', admin.site.urls), - path('', views.blog), + path('', views.all_articles), ]