当前路径 chat/room/1/ 与其中任何一个都不匹配

Posted

技术标签:

【中文标题】当前路径 chat/room/1/ 与其中任何一个都不匹配【英文标题】:The current path, chat/room/1/, didn't match any of these 【发布时间】:2021-02-12 09:16:34 【问题描述】:

我正在使用 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.” **

我真的不知道怎么了。请有人帮助我。谢谢。 以下是文件:

educa/rules.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 %

educa/settings.py:

INSTALLED_APPS = [
     ...  
   'chat',
   'channels',

]

【问题讨论】:

显示用于呈现聊天室链接的 html 【参考方案1】:

试试这个

urlpatterns = [ 
   path('room/<int:course_id>/', views.course_chat_room, name='course_chat_room'),
]

更多关于网址https://docs.djangoproject.com/en/3.1/ref/urls/#path

【讨论】:

【参考方案2】:
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 %

【讨论】:

以上是关于当前路径 chat/room/1/ 与其中任何一个都不匹配的主要内容,如果未能解决你的问题,请参考以下文章

找不到页面 (404) 请求方法:GET .......当前路径,博客/博客文章,与其中任何一个都不匹配

添加新记录时出错当前路径 blog_app/android/add/ 与其中任何一个都不匹配。 - django

当前路径 Envs/python-projects/learnpy/static/styles/bootstrap4/bootstrap.min.css 与其中任何一个都不匹配

python 获取当前,上级,上上级路径(任何上级路径)

将链接添加到按钮时,当前路径与这些错误中的任何一个都不匹配

当前 URL app/ 与其中任何一个都不匹配