Django是反向的,url是默认的,而不是NoReverseMatch

时间:2021-09-06 18:51:10

Is it possible to set up a default URL which will be used whenever a reverse match cannot be found? The idea is that if in production there is a typo, I would like to display something akin to a 404 or a descriptive error page, instead of getting a NoReverseMatch exception.

是否可以设置一个默认URL,当查找不到反向匹配时使用该URL ?其思想是,如果在生产中出现了一个错误,我希望显示类似于404或描述性错误页面的内容,而不是获得NoReverseMatch异常。

2 个解决方案

#1


1  

The templatetag url raise a Exception. You can see in the code:

templatetag url引发一个异常。你可以在代码中看到:

https://code.djangoproject.com/browser/django/trunk/django/templatetags/future.py#L117

https://code.djangoproject.com/browser/django/trunk/django/templatetags/future.py L117

But, you can create other templatetag (copy and paste 90%), that does not raise anything and "display something akin to a 404 or a descriptive error page, instead of getting a NoReverseMatch exception."

但是,您可以创建其他的templatetag(复制并粘贴90%),这不会引起任何问题,并且“显示类似于404或描述性错误页面的内容,而不会出现NoReverseMatch异常”。

#2


1  

i guess you could catch the exception and choose your detault url

我猜您可以捕获异常并选择您的detault url

try:
    url = reverse('missing')
except NoReverseMatch:
    url = default

#1


1  

The templatetag url raise a Exception. You can see in the code:

templatetag url引发一个异常。你可以在代码中看到:

https://code.djangoproject.com/browser/django/trunk/django/templatetags/future.py#L117

https://code.djangoproject.com/browser/django/trunk/django/templatetags/future.py L117

But, you can create other templatetag (copy and paste 90%), that does not raise anything and "display something akin to a 404 or a descriptive error page, instead of getting a NoReverseMatch exception."

但是,您可以创建其他的templatetag(复制并粘贴90%),这不会引起任何问题,并且“显示类似于404或描述性错误页面的内容,而不会出现NoReverseMatch异常”。

#2


1  

i guess you could catch the exception and choose your detault url

我猜您可以捕获异常并选择您的detault url

try:
    url = reverse('missing')
except NoReverseMatch:
    url = default