2

I want a simple edit / remove form in Django.

I want it to look like:

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

I want to the edit and remove "buttons" to be hyperlinks, or at least look like them.

Is there an easy way without multiple submit buttons (and without violating the whole POST/GET thing?)

3 Answers 3

2

You would probably do better not using a Form to achieve this, as there are (from what you've described) no form elements required.

Instead you could have a setup 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'),

And the interface you described above is generated by a template which simply uses {% url %} tag to make hyperlinks to those addresses. Say, for example, you are passing the variable 'items' in your context, you template code would look like this

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

...or something to all that effect...

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. I've implemented this. Although it uses GET, and hence breaking the standards a bit (using a GET to do a delete from a db). How can you follow the standards when it is so difficult? (without coding a multi submit form with CSS controlled submit buttons)...
2

I did something like this for a similar problem: put this javascript in your template:

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

use your template to create this delete hyperlink for each item:

<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 item will have a delete hyperlink which calls the js submit function and passes the item to delete. The js function submit sets a hidden input value to this item, then submits the form with that input thereby passing the value via POST to the url /view_to_handle_delete/, and there you handle it like a normal post request. Here the item_id will be called item_to_delete.

Comments

1

Create separate form elements for each entry. Include the item.id as a hidden field as you iterate over each item's form in the template.

This is a simple way to use POST-based delete over multiple items, important if you'd like to prevent CSRF attacks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.