Django add / remove form without multiple submit

I need a simple edit / delete form in Django.

I want it to look like this:

Item A   edit  /   remove
Item B   edit  /   remove
Item C   edit  /   remove

I want to edit and delete the β€œbuttons” to be hyperlinks, or at least look like them.

Is there an easy way without a few submit buttons (and without breaking just the POST / GET?)

+1
source share
3 answers

Perhaps you are better off not using a form to achieve this, as there are (from what you described) no form elements are required.

Instead, you might have a setting in which your urls.py has 2 URLs,

url(r'^app/edit/(?P<id>.*)$', edit_view, name='item_edit'),
url(r'^app/remove/(?P<id>.*)$', remove_view, name='item_remove'),

, {% url%} . , , "" , :

<table>
{% for item in items %}
    <tr>
      <td>{{item.name}}</td>
      <td>{% url 'item_edit' item.id %}</td>
      <td>{% url 'item_remove' item.id %}</td>
    </tr>
{% endfor %}
</table>

... - ...

+2

. item.id , .

POST , , CSRF.

+1

- : javascript :

<script language="javascript">function submit(item_id){
document.myform.item_to_delete.value = item_id;
document.myform.submit();
}
</script>

:

<a href="javascript:submit({{ item_id }});">Delete</a>

put this hidden form somewhere on your page:

<form name="myform" method="post" action="/view_to_handle_delete/"><input name="item_to_delete" type="hidden" value=""></form>'

basically, each element will have a delete hyperlink that calls the js submit function and passes the element to delete. The js submit function assigns a hidden input value to this element, then submits a form with this input, passing the value through POST to url / view_to_handle_delete /, and there you process it as a regular mail request. Here item_id will be called item_to_delete.

+1
source

Source: https://habr.com/ru/post/1717507/


All Articles