问题很简单。如果我想检查网址是否像:
http://127.0.0.1:8000/discussions
是活跃的,我会这样做,
{% url 'discussions:index' as discussions %}
<li class="nav-item {% if request.get_full_path == discussions %}active{% endif %}">
<a class="nav-link" href="{% url 'discussions:index' %}">Discussions </a>
</li>
但我想为此网址启用相同的导航栏链接:
http://127.0.0.1:8000/discussions/Q15
但我在导航栏模板中没有问题ID(Q15)。我应该如何比较它,以便所有类型的URL变为活动(真实条件):
http://127.0.0.1:8000/discussions/anything/anything2
urls.py
app_name = 'discussions'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>Q[0-9]+)$', views.question, name='question'),
url(r'^ask/', views.ask, name='ask'),
url(r'^answer/(?P<question_id>Q[0-9]+)$', views.answer, name='answer'),
url(r'^delete/(?P<id>[AQ][0-9]+)', views.delete, name='delete'),
url(r'^edit/(?P<id>[AQ][0-9]+)', views.edit, name='edit'),
url(r'^confirm/(?P<id>[AQ][0-9]+)', views.confirm, name='confirm'),
url(r'^vote/', views.vote, name='vote'),
}
我希望所有人都这样。
答案 0 :(得分:2)
试试这个:
{% if discussions in request.get_full_path %}active{% endif %}
希望它有所帮助!