python - re_path 未解析的正则表达式 (\d[1,2])
问题描述
请让我知道为什么我的正则表达式无法解析,我只是从一本 Django 书籍中复制并粘贴,并尝试传递参数re_path(r'^time/plus/(\d[1,2])/$', hours_ahead)
。只是试图将参数额外的时间传递给当前时间。
http://127.0.0.1:8000/time/plus/2
然后我得到了找不到页面(404)错误。当前路径 time/plus/2 与其中任何一个都不匹配。
我不明白这里有什么问题。请帮忙,谢谢。
解决方案
这是因为如果你只需要一个和两个这样的数字,这是错误的正则表达式,所以没有必要像这样指定
case 1:
only 1 and 2 is accept
want:time/plus/2/ --pass
want:time/plus/1/ --pass
want:time/plus/3/ --fail
regex: r'^time/plus/[1,2]/$'
case 2:
length of that is 1 or 2
want:time/plus/2/ --single digit -pass
want:time/plus/33/ --two digit -pass
want:time/plas/333/ --three digit -fail
regex: r'^time/plus/\d{1,2}/$'
推荐阅读
- linux - 无法卸载并重新安装 Truffle?
- android - 项目错误:您需要将 ANDROID_NDK_ROOT 环境变量设置为指向您的 Android NDK
- php - Laravel Spatie 角色和权限:如何更改默认表?
- c# - 将数组拆分为大约等于总和而不重新排列
- algorithm - 最长递增子序列变化动态规划
- c# - Cosmos DB 中是否支持使用 OData 进行分页?
- javascript - 在 React 中,我可以在另一个功能组件的主体中定义一个功能组件吗?
- google-app-engine - 无法匹配 Google App Engine 和 VPC 网络之间的区域
- c# - guflow 错误 ACTIVITY_ID_ALREADY_IN_USE aws
- python - 在 python 中捕获和处理特定异常