This is what you would normally want to do in the views.py file using reverse () for named URLs with known arguments or resolve () for paths.
If you really need this functionality in a template, here is a hacky solution:
@register.simple_tag
def urlpath_exists(name):
"""Returns True for successful resolves()'s."""
try:
return bool(resolve(path))
except Resolver404:
return False
Note : this does not guarantee that the URL is valid, just match the pattern.
source
share