Html 在Django中将链接传递作为POST方法

Html 在Django中将链接传递作为POST方法,html,django,Html,Django,对于GET和POST方法,我有一个不同的视图,对于包含2个指向相应URL的链接的页面,我有以下模板 <!DOCTYPE html> <html> <head> <script language="JavaScript" type="text/javascript"> function getsupport ( selectedtype ) { document.create_station.supporttype.va

对于GET和POST方法,我有一个不同的视图,对于包含2个指向相应URL的链接的页面,我有以下模板

<!DOCTYPE html>
<html>
<head>
   <script language="JavaScript" type="text/javascript">
    function getsupport ( selectedtype )
    {
    document.create_station.supporttype.value = selectedtype ;
    document.create_station.submit() ;
    }
  </script>
</head>

<body>
  <h1> Hey {{ object.username }}! </h1>
  <p><a href="{% url 'list_create_station' %}">View your stations</a>   </p>
  <form name="create_station" method="post" action="">
    <input type="hidden" name="supporttype" />
    <a href="{% url 'list_create_station' %}">Create a new Station</a>
  </form>
</body>
</html>

函数getsupport(selectedtype)
{
document.create\u station.supporttype.value=selectedtype;
document.create_station.submit();
}
嘿{{object.username}}!

我试图让第二个链接传递一个POST方法,而不是从下面的代码中获得帮助

但该链接仍然通过了GET,并使用Chromium开发工具对其进行了验证


我是一个新手,因此从参考中复制了代码。我了解一些细节,所以请有人给出相应的答案。如果有人确切地告诉我我在这里做错了什么,并向我解释我必须做哪些更改,那将是一件非常美妙的事情。

您表单的代码应该是:

<form name="create_station" method="post" action="{% url 'list_create_station' %}">
    <input type="hidden" name="supporttype" />
    <input type="submit" value="Create a new Station" />
</form>

您可以找到有关“提交”按钮和“操作”属性的信息


基本上,您需要一个
submit
按钮来提交表单,否则
input
字段中的数据将不会发送到下一个视图。
action
属性指示哪个视图应该处理表单。如果
action
为空,您的表单将被发送到当前使用的同一视图,但使用表单中定义的
方法。

您也可以将代码编辑为类似于下面的内容

<form class="success" id="form-id" action="{% url "formly_dt_page_create" pk=selected_survey.pk %}" method="post">
    {% csrf_token %}
    <a class="nav-link {% if page == selected_page %}active{% endif %}" onclick="document.getElementById('form-id').submit();">Add page</a>
</form>

{%csrf_令牌%}
添加页面
您应该给表单一个id,以便链接中的操作可以直接针对表单

例如,在上面的示例中,我给表单一个
id=“form id”
id
,并将值传递给操作
onclick=“document.getElementById('form-id')。submit();”


我真的希望这能帮助别人

记住,你可以使用CSS使按钮看起来像超链接。而且隐藏的输入是不必要的,JavaScript也是不必要的。我想我们可以通过某种方式让“链接”发帖。但这对我来说已经足够了。@Filly请退房