使Django中的链接传递POST方法 [英] Making a link pass a POST method in Django

查看:182
本文介绍了使Django中的链接传递POST方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a view acting differently for GET and POST methods and I have the following template for a page containing 2 links to the corresponding 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>

我试图使第二个链接通过POST方法而不是GET,从< a href =http://www.thesitewizard.com/archive/textsubmit.shtml =nofollow> http://www.thesitewizard.com/archive/textsubmit.shtml

I am trying to make the second link pass a POST method instead of GET with help from code at http://www.thesitewizard.com/archive/textsubmit.shtml

但链接仍然通过GET,使用Chromium开发人员工具进行验证。

But the link still passes a GET, verified it using Chromium developer tools.

我是一个新手,因此已经真正复制了参考的代码。我明白了点和部分,所以请有人给出答案。如果有人告诉我我在这里做错了什么,那么可能会很可爱,并向我解释我必须做的更改。

I am a novice and hence have literally copied the code from the reference. I understand bits and parts so please someone give an answer accordingly. It would be lovely if someone tells me exactly what I am doing wrong here and explain to me what changes exactly I have to make.

推荐答案

p>您的表单的代码应该是:

The code for your form should be:

<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>

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

You can find information about the submit button and the action attribute here.

基本上,您需要一个提交按钮提交您的表单,否则您的输入字段内的数据将不会发送到下一个视图。 操作属性指示应该处理表单的视图。如果操作为空,您的表单将被发送到您正在使用的同一视图,但使用方法在表格中定义。

Basically, you need a submit button to submit your form, otherwise the data inside your input fields won't be send to the next view. The action attribute indicates what view should handle the form. If action is blank, your form will be sent to the same view you're using right now, but using the method defined in the form.

这篇关于使Django中的链接传递POST方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆