【问题标题】:Django: call a view function via a button clickDjango:通过单击按钮调用视图函数
【发布时间】:2020-12-22 13:26:49
【问题描述】:

我想通过单击 html 按钮来调用视图函数 resign_game。这是我在阅读了一些 Stackoverflow 帖子后尝试过的:

<form action="{% url 'resign_game' %}" method="POST">
     <button type="button">RESIGN</button>
</form>

这是我的 urls.py 中的内容:

path('resign_game/', views.resign_current_game, name='resign_game')

但是,在我单击按钮后,视图函数从未被调用。我会很感激你的建议。谢谢!

【问题讨论】:

标签: python html django


【解决方案1】:

带有type="button"button 元素不发送表单。将类型设置为“提交”:

 <button type="submit">RESIGN</button>

或者去掉类型:

 <button>RESIGN</button>

【讨论】:

    猜你喜欢
    • 2016-10-14
    • 2017-10-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 2016-09-16
    • 2020-08-29
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多