python-3.x - 当前路径 chat/room/1/ 与其中任何一个都不匹配
问题描述
我正在使用 Django 3 进行练习,并且我有一个名为 educa 的聊天项目。我尝试通过 python manage.py runserver 运行项目并访问 http://127.0.0.1:8000/chat/room/1/。我总是收到以下错误消息:
“Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/chat/room/1/
Using the URLconf defined in educa.urls, Django tried these URL patterns, in this order:
1.accounts/login/ [name='login']
2.accounts/logout/ [name='logout']
3.admin/
4. course/
…
** The current path, chat/room/1/, didn't match any of these.” **
我真的不知道怎么了。请有人帮助我。谢谢。以下是文件:
教育/规则.py:
urlpatterns = [
path('chat/', include('chat.urls', namespace='chat')),
]
聊天/urls.py:
app_name = 'chat'
urlpatterns = [
path('room/<int:course_id>/', views.course_chat_room, name='course_chat_room'),
]
聊天/views.py:
@login_required
def course_chat_room(request, course_id):
...
return render(request,'chat/room.html', {'course': course})
聊天/模板/聊天/room.html:
{% extends "base.html" %}
{% block title %}Chat room for "{{ course.title }}"{% endblock %}
{% block content %}
….
{% endblock %}
{% block domready %}
...
{% endblock %}
教育/设置.py:
INSTALLED_APPS = [
...
'chat',
'channels',
]
解决方案
I have tried:
" path('room/<int:course_id>/', views.course_chat_room, name='course_chat_room'),
but the result is all the same. I think it doesn't solve my problem.
**html that I used to render the link to the chatroom:**
{% extends "base.html" %}
{% block title %}Chat room for "{{ course.title }}"{% endblock %}
{% block content %}
<div id="chat">
</div>
<div id="chat-input">
<input id="chat-message-input" type="text">
<input id="chat-message-submit" type="submit" value="Send">
</div>
{% endblock %}
{% block domready %}
var url = 'ws://' + window.location.host +
'/ws/chat/room/' + '{{ course.id }}/';
var chatSocket = new WebSocket(url);
chatSocket.onmessage = function(e) {
var data = JSON.parse(e.data);
var message = data.message;
var dateOptions = {hour: 'numeric', minute: 'numeric', hour12: true};
var datetime = new Date(data.datetime).toLocaleString('en', dateOptions);
var isMe = data.user === '{{ request.user }}';
var source = isMe ? 'me' : 'other';
var name = isMe ? 'Me' : data.user;
var $chat = $('#chat');
$chat.append('<div class="message ' + source + '">' +
'<strong>' + name + '</strong> ' +
'<span class="date">' + datetime + '</span><br>' +
message +
'</div>');
$chat.scrollTop($chat[0].scrollHeight);
};
chatSocket.onclose = function(e) {
console.error('Chat socket closed unexpectedly');
};
var $input = $('#chat-message-input');
var $submit = $('#chat-message-submit');
$submit.click(function() {
var message = $input.val();
if(message) {
// send message in JSON format
chatSocket.send(JSON.stringify({'message': message}));
// clear input
$input.val('');
// return focus
$input.focus();
}
});
$input.focus();
$input.keyup(function(e) {
if (e.which === 13) {
// submit with enter / return key
$submit.click();
}
});
{% endblock %}
推荐阅读
- kubernetes - 在 k8s pod 中应用任务
- reactjs - 在 reactjs 中为所有相同类型的组件设置样式属性
- r - 如何过滤 R 中具有特定值的行?
- c# - 无法从 ASP.NET Core 2.1 控制器获取 IdentityUser 数据
- git - 使用 TortoiseGit 清除旧的提交
- ios - 角度 6 - 最新 ios 上的 3d touch 问题
- tensorflow - TensorFlow 中的“Flex Op”是什么意思?
- bash - 使用 AppleScript 或 Automator 从文本文件中查找和复制带有文本字符串的图像,并将图像复制到另一个文件夹
- ios - Xcode - 更改框架的搜索路径
- sql-server - 为什么我们需要删除表来重新链接它们?