2017-08-12 51 views
0

問題很簡單。如果我要檢查,如果喜歡的網址:檢查Django url是否存在或其中的一部分存在?

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'), 
} 

回答

2

試試這個:

{% if discussions in request.get_full_path %}active{% endif %} 

希望它能幫助!