我正在按照教程构建一个聊天室应用程序。当我进入一个房间时,url看起来像这样http://127.0.0.1:8000/chatroomname/?username=username'. 我想为每个聊天室添加不同的视频,我想在room.html模板中使用和{%if%}标记:
{% if request.get_full_path == "/roomname/" %}
<iframe width="560" height="315" src="https://www.youtube.com/watch?v=uilkmUoXoLU" frameborder="0" allowfullscreen></iframe>
{% endif %}
但这只有在url看起来像这样时才有效http://127.0.0.1:8000/chatroomname/,如何在if标记中添加用户名部分。
如果我添加一个示例用户名并使用该用户名访问房间,视频将不会显示。
{% if request.get_full_path == "/roomname/?username=exampleusername" %}
<iframe width="560" height="315" src="https://www.youtube.com/watch?v=uilkmUoXoLU" frameborder="0" allowfullscreen></iframe>
{% endif %}
这是其他文件的代码:
# views.py
from django.shortcuts import render
from .models import Message
def index(request):
return render(request, 'chat/index.html')
def room(request, room_name):
username = request.GET.get('username', 'Anonymous')
messages = Message.objects.filter(room=room_name)[0:25]
return render(request, 'chat/room.html', {'room_name': room_name, 'username': username, 'messages': messages})
# urls.py
from django.urls import path
from .models import Message
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('<str:room_name>/', views.room, name='room'),
]
# models.py
from django.db import models
class Message(models.Model):
username = models.CharField(max_length=255)
room = models.CharField(max_length=255)
content = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('date_added',)
# routing.py
from django.urls import path
from . import consumers
websocket_urlpatterns = [
path('ws/<str:room_name>/', consumers.ChatConsumer.as_asgi()),
]
# consumers.py
import json
from channels.generic.websocket import AsyncWebsocketConsumer
from asgiref.sync import sync_to_async
from .models import Message
class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = 'chat_%s' % self.room_name
# Join room
await self.channel_layer.group_add(
self.room_group_name,
self.channel_name
)
await self.accept()
async def disconnect(self, close_code):
# Leave room
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
# Receive message from web socket
async def receive(self, text_data):
data = json.loads(text_data)
message = data['message']
username = data['username']
room = data['room']
await self.save_message(username, room, message)
# Send message to room group
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'chat_message',
'message': message,
'username': username
}
)
# Receive message from room group
async def chat_message(self, event):
message = event['message']
username = event['username']
# Send message to WebSocket
await self.send(text_data=json.dumps({
'message': message,
'username': username
}))
@sync_to_async
def save_message(self, username, room, message):
Message.objects.create(username=username, room=room, content=message)
暂无答案!
目前还没有任何答案,快来回答吧!