如何通过request.path检查包含参数的路径?

问题描述 投票:1回答:1

在我的questions应用中,url_patterns中的models.py是:

urlpatterns = [    
    path('<int:question_id>/'     , views.question    , name='question'),
    path('new/'                   , views.new_question, name='newquestion'),    
    path('edit/<int:question_id>/', views.edit        , name='edit'),    
]

例如,在我的模板中,每当我想检查newquestion网址是否已加载时,我都尝试这样做:

{% if 'questions/new' in request.path %}
    some code
{% endif %}

[如何检查question网址是否包含整数参数-question_id-

django django-templates
1个回答
0
投票

如果确实需要,可以使用request.resolver_match [Django-doc]获取request.resolver_match,然后获取ResolverMatch object [Django-doc]以获取ResolverMatch.view_name attribute [Django-doc] .view_name等的名称

© www.soinside.com 2019 - 2024. All rights reserved.