urlpattern Regex is not working as expected

huangapple go评论69阅读模式
英文:

urlpattern Regex is not working as expected

问题

urlpatterns = [ path(r'/.*', maintenance_view, name='maintenance') ]

英文:

I have a Django website and for the times that pages are not ready I want to redirect any URL to a specific maintenance page.

So in the urlpatterns of my website I added this regex expecting it to capture anything after / but it's not working.
urlpatterns = [
path(r'/.*',maintenance_view,name='maintenance')
]

答案1

得分: 0

我自己找到了答案。问题是我必须使用re_path,而且在正则表达式中,Django 不关心“/”,所以我将其移除。

from django.urls import re_path    
urlpatterns=[
    re_path(r'.*', maintenance_view, name='maintenance')
]
英文:

I found the answer myself. The problem was that I had to use re_path and also in the regex django was not caring about the "/", so I removed it.

from django.urls import re_path    
urlpatterns=[
re_path(r'.*',maintenance_view, name='maintenance')
]

huangapple
  • 本文由 发表于 2023年2月6日 13:15:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定