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.

asked 22 hours ago
6
  • 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 19 hours ago
  • 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 17 hours ago
  • use \\w+ , so change to re_path(r"ws/chat/(?P<room_name>\\w+)/$", consumers.ChatConsumer.as_asgi()) Commented 16 hours ago
  • @sahasrara62 Inside of a raw string, \\w would have two actual backslashes, which would not be correct in this context. Commented 7 hours ago
  • @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 28 mins ago

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.