Django-如何直接从表格中的按钮删除对象 [英] Django - How to delete a object directly from a button in a table

查看:73
本文介绍了Django-如何直接从表格中的按钮删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(抱歉,我的英语不好)

(sorry for my bad english)

我需要删除一个对象,但直接从y模板中包含的对象列表中删除。

I need to delete an object, but directly from a list of the objects that y have in my template.

我有一个工单,其中有备件,但是我不知道如何仅在工单的detailview中使用一个丁字创建备件的deleteview。想法是用户单击删除按钮。

I have a work orders, that have spare parts but i don't know how to create the deleteview for the spare parts using only a buton in the detailview of the work order. The idea is that the user make click in the Delete button.

这是备件的模型

class OrderSparePart(models.Model):
    # Relations
    workorder = models.ForeignKey(
            WorkOrder,
            verbose_name=_('order'),
        )
    # Attributes - Mandatory
    spare_part = models.CharField(
            max_length=80,
            verbose_name=_('spare part'),
        )
    # Attributes - Optional
    price = models.DecimalField(
            max_digits=6,
            decimal_places=2,
            null=True,
            blank=True,
            verbose_name=_('price'),
        ) 
    # Object Manager
    # Custom Properties
    # Methods
    def get_absolute_url(self):
        return reverse('work_orders:detail', kwargs={'order_id': self.workorder.id})

    # Meta and String
    class Meta:
        verbose_name = _("order spare part")
        verbose_name_plural = _("order spare parts")

这是模板中显示的位置

                  {% if spare_parts %}
                  <table class="table">
                    <thead>
                      <tr>
                        <th>{% trans "Spare Part" %}</th>
                        <th>{% trans "Price" %}</th>
                        <th>{% trans "Delete" %}</th>
                      </tr>
                    </thead>
                    <tbody>
                      {% for part in spare_parts %}
                      <tr>
                        <td><i class="fa fa-gear"></i> {{ part.spare_part }}</td>
                        {% if part.price %}
                        <td>$ {{ part.price }}</td>
                        {% else %}
                        <td></td>
                        {% endif %}
                        <td><a href="#"><i class="fa fa-trash"></i></a></td>
                      </tr>
                      {% endfor %}
                    </tbody>
                  </table>
                {% else %}
                <p>NO HAY REPUESTOS ASENTADOS AÚN</p>
                {% endif %}

这个想法是使用删除备件。

The the idea is use the to delete the spare part.

我如何制作Deleteview及其链接?

how i have to make the deleteview and the link to this???

谢谢!

推荐答案

在fa fa-thrash中,按我的方式传递ID和URL:-

here in fa fa-thrash pass the id and the URL as I did it:-

{% if spare_parts %}
              <table class="table">
                <thead>
                  <tr>
                    <th>{% trans "Spare Part" %}</th>
                    <th>{% trans "Price" %}</th>
                    <th>{% trans "Delete" %}</th>
                  </tr>
                </thead>
                <tbody>
                  {% for part in spare_parts %}
                  <tr>
                    <td><i class="fa fa-gear"></i> {{ part.spare_part }}</td>
                    {% if part.price %}
                    <td>$ {{ part.price }}</td>
                    {% else %}
                    <td></td>
                    {% endif %}
                    <td><a href="url:delete_view" part.id><i class="fa fa-trash"></i></a></td>
                  </tr>
                  {% endfor %}
                </tbody>
              </table>
            {% else %}
            <p>NO HAY REPUESTOS ASENTADOS AÚN</p>
            {% endif %}

您的网址看起来像这样:

ur url would be sonething like that:

url(r'^delete/(?P<part_id>[0-9]+)/$', view.function, name='delete_view'),

在您的视图中:

def function(request,part_id =None):
    object = YourModel.objects.get(id=part_id)
    object.delete()
    return render(request,'ur template where you want to redirect')

这篇关于Django-如何直接从表格中的按钮删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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