首页 > 解决方案 > 如何在下拉菜单的选项标签内使用 django url?

问题描述

我有一个带有两个选项的选择标签。我希望根据所选选项更改 url。我希望将 django 中 URLPATTERNS 中定义的 url 链接到这些选项。我的html:

 <select name="rec-type" id="rec-type">
    <!-- <a href="{% url 'transactions' rec_type='expense' %}"> -->
    <option value="expense">Expense</option>
    <!-- </a> -->
    <!-- <a href="{% url 'transactions' rec_type='expense' %}"> -->
    <option value="income">Income</option>
    <!-- </a> -->
  </select>

我的交易网址是:

    path('transactions/<str:rec_type>', views.transactions, name='transactions'),

标签: htmldjangohtml-select

解决方案


<select name="rec-type" id="rec-type" onchange="location = this.value;">

        <option value="{% url 'transactions' "expense" %}">Expense</option>
        <option value="{% url 'transactions' "income" %}">Income</option>

</select>

这应该可以,您在更改选项时会转到此网址


推荐阅读