0

As I am following the Django channels tutorial (here is the link: https://channels.readthedocs.io/en/latest/tutorial/part_2.html ), I won't post any code except the specific problem I am having.
In my code for the chat/routing.py, I have:

from django.urls import re_path

from . import consumers

websocket_urlpatterns = [
    re_path(r'ws/chat/(?P<room_name>\w+)/$', consumers.ChatConsumer.as_asgi()),
]

which seems to be identical to that which is given in the tutorial. Trying both single and double quotes, I get the error:

SyntaxWarning: invalid escape sequence '\w'
  r"ws/chat/(?P<room_name>\w+)/$", consumers.ChatConsumer.as_asgi()

Everything I can find says one is supposed to pre-pend the r to make the expression a regular expression, but clearly I have done so to no effect. What am I doing wrong? Have I missed something? I have copied the line from the tutorial and compared it to mine and it is identical. Thanks in advance.

8
  • 1
    My best guess is that you're looking at the wrong place. To verify this, you can try changing \w to \d(or something else) and see if the warning still says \w. Commented Nov 18 at 4:14
  • This question is similar to: How to fix "SyntaxWarning: invalid escape sequence" in Python?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Nov 18 at 6:49
  • use \\w+ , so change to re_path(r"ws/chat/(?P<room_name>\\w+)/$", consumers.ChatConsumer.as_asgi()) Commented Nov 18 at 7:17
  • @sahasrara62 Inside of a raw string, \\w would have two actual backslashes, which would not be correct in this context. Commented Nov 18 at 16:37
  • @DmitryLeiko: my question differs from the one you mention because I have pre-pended the r character. As I mentioned, this is the only solution I have found, and I get the error even with it. Commented Nov 18 at 23:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.