html - 如何在下拉菜单的选项标签内使用 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'),
解决方案
<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>
这应该可以,您在更改选项时会转到此网址
推荐阅读
- netsuite - NetSuite 生成数字签名以调用 restlet
- swift - CoreData:不调用 ValueTransformer 函数
- excel - 请参阅动态工作簿
- makefile - Makefile if 语句导致一些奇怪的行为
- laravel - 更改外键而不丢失列中的数据
- javascript - 从 json 数组计算变量中的总值
- c# - 清除了 nuget locals,现在如何撤消它?
- docker - 无法将音量链接到 linux 上的 rocker/rstudio 容器
- ubuntu - 消息“无法运行 arm-none-eabi-gdb:找不到 libncurses.so.5”
- windows - 使用 cmd 重命名具有某些字符的文件