如何检查特定的URL保留匹配是否有效?

iTa*_*ayb 3 django django-templates

{% url form_entry %}我的模板中有一个标签,而form_entry值来自数据库.

如果网址无法解决,我会得到一个NoReverseMatch.如何在实际运行标记并获得异常之前检查标记是否成功?

就像是:

{% if resolvable form_entry %}
    <a href="{% url form_entry %}">click here</a>
{% else %}
    Sorry, not found.
{% endif %}
Run Code Online (Sandbox Code Playgroud)

Ala*_*air 5

使用标记as选项url.

{% url form_entry as the_url %}
{% if the_url %}
<a href="{{ the_url }}">click here</a>
{% else %}
Sorry, not found.
{% endif %}
Run Code Online (Sandbox Code Playgroud)