首页 > 解决方案 > 当前路径 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',

]

标签: python-3.xdjangochatroom

解决方案


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 %}

推荐阅读