2015-05-14 21 views
0

我有GET和POST方法行事不同的景色,我有包含2個鏈接到相應的URL製作一個鏈接通過POST方法在Django

<!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在代碼幫助http://www.thesitewizard.com/archive/textsubmit.shtml

但鏈接仍然通過GET,使用Chromium開發人員工具對其進行驗證。

我是一個新手,因此從字面上複製了引用中的代碼。我瞭解零件和零件,所以請有人給出相應的答案。如果有人告訴我我在做什麼錯誤,並向我解釋我必須做出什麼改變,那將是可愛的。

回答

3

爲您的表單的代碼應該是:

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

您可以找到有關提交按鈕和動作屬性here信息。

基本上,您需要一個submit按鈕來提交您的表單,否則您input字段中的數據將不會發送到下一個視圖。 action屬性指示哪個視圖應該處理該表單。如果action爲空白,表單將被髮送到您現在使用的相同視圖,但使用表單中定義的method

+0

請記住,您可以使按鈕看起來像使用CSS的超鏈接。 –

+0

隱藏的輸入也是不必要的,JavaScript也是如此。我想我們可以通過某種方式實際發佈「鏈接」。但是這對我來說已經足夠了。 –

+0

@Filly請查看http://stackoverflow.com/questions/30243865/django-get-got-an-unexpected-keyword-argument-pk-error –

0

您也可以將代碼編輯爲類似於下面的代碼。

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

您應該給表單一個id,以便鏈接中的動作可以直接指向表單。

例如從上面的例子中我已經給出的形式的一個id="form-id"id傳遞的值給動作onclick="document.getElementById('form-id').submit();"

我確實希望這會幫助別人。