django URL 反转:当 URL 反转用户名时,如果用户名包含“.”,则会失败。里面的字面意思

发布于 2024-09-11 04:24:15 字数 174 浏览 5 评论 0原文

我没想到会发生这种情况[因为我不知道 django 何时更改为允许 _ 和 .在用户名中],但是当我尝试时 {% url feed_user entry.username %}

当用户名包含“.”时,我会收到 500 错误 在这种情况下 rob.e 作为用户名将会失败。

有什么想法如何处理这个问题吗?

I didn't expect this to occur [since I didn't know when django changed to allow _ and . in usernames], but when I attempt
{% url feed_user entry.username %}

I will get a 500 error when the username contains a '.'
In this case rob.e as a username will fail.

Any ideas how to deal with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小伙你站住 2024-09-18 04:24:16

问题在于您在 urls.py 中使用的任何正则表达式来匹配 feed_user。大概您正在使用类似 r'(?P\w+)/$' 的内容,它仅匹配字母数字字符,不匹配标点符号。

相反,请使用:r'(?P[\w.]+)/$'

The problem will be in whatever regex you are using in your urls.py to match feed_user. Presumably you are using something like r'(?P<username>\w+)/$', which only matches on alphanumeric characters and doesn't match on punctuation.

Instead, use this: r'(?P<username>[\w.]+)/$'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文