Django 使用正则表达式时的混乱URL

Django 使用正则表达式时的混乱URL,django,django-urls,Django,Django Urls,Im使用django allauth,最初用户可以通过访问网站/profile访问他们的配置文件,URL.py是路径('profile/',include('users.url'))。后来我决定希望用户查看其他用户配置文件,因此我需要将url.py更改为路径(r'^profile/(?P[\w-]+)/$,include('users.url')) 问题是,现在当用户访问配置文件时,不是像website/profile/user1这样漂亮、干净的url,而是像这样的website/%5Eprof

Im使用django allauth,最初用户可以通过访问
网站/profile
访问他们的配置文件,URL.py是
路径('profile/',include('users.url'))
。后来我决定希望用户查看其他用户配置文件,因此我需要将url.py更改为
路径(r'^profile/(?P[\w-]+)/$,include('users.url'))

问题是,现在当用户访问配置文件时,不是像
website/profile/user1
这样漂亮、干净的url,而是像这样的
website/%5Eprofile/(%3FP1%5B%5Cw-%5D+)/$

这可能不是问题,但我更喜欢在我的网站地址栏中有一个干净的url,并且认为这可能表明我的实现不正确

谢谢。

使用重复路径或缓动模式
re_路径(r'^profile/(?P[\w-]+)/$),包括('users.url'))
#或
路径('profile/',include('users.url'))

^
%5E
urlencoded,依此类推,还有剩余的额外字符

您将旧的正则表达式路径(or)与新语法混淆

如果要保留路径,请执行以下操作:

path('profile/<username>', include('users.urls'))
path('profile/',include('users.url'))
你也可以检查你是否需要定制一个

re_路径(r'^profile/(?P[\w-]+)/$),包括('users.url'))

这几乎是正确的,但您不应该在常规路径语法中使用regex的start和end字符
path('profile/<username>', include('users.urls'))
re_path(r'^profile/(?P<user_id>[\w-]+)/$', include('users.urls'))