Django添加/删除表单,无需多次提交 [英] Django add / remove form without multiple submit

查看:114
本文介绍了Django添加/删除表单,无需多次提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个简单的编辑/删除表单在Django。



我希望它看起来像:

 项目A编辑/删除
项目B编辑/删除
项目C编辑/删除

我想编辑和删除按钮为超级链接,或至少看起来像他们。



是否一个简单的方法没有多个提交按钮(并且不违反整个POST / GET的东西?)

解决方案

你可能会做得更好使用表单来实现这一点,因为有(从你所描述的)没有表单要素。



相反,你可以有一个设置,其中你的urls.py有2个网址,

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

上面描述的界面是由模板生成的它只是使用{%url%}标签来创建超链接到这些地址。比方说,你在你的上下文中传递变量'items',你的模板代码看起来就像这样

 表> 
{%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>

...或所有这些效果...


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

解决方案

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

这篇关于Django添加/删除表单,无需多次提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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