Django - 如何直接从表中的按钮删除对象

时间:2017-05-29 18:02:47

标签: django

(抱歉我的英语不好)

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

我有工作订单,有备件,但我不知道如何在工作单的详细信息视图中仅使用buton创建备件的deleteview。想法是用户点击“删除”按钮。

这是备件的型号

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

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

我如何制作deleteview及其链接???

谢谢!

2 个答案:

答案 0 :(得分:4)

html template inside for循环中,使用<td>中的表单标记创建删除按钮,如下所示(如果使用bootstrap3,css类将起作用):

<form action="{% url 'delete_view' pk=part.pk %}" method="POST">
  {% csrf_token %}
  <input class="btn btn-default btn-danger" type="submit" value="Delete"/>
</form>

urls.py

中添加urlpattern
url(r'^delete-entry/(?P<pk>\d+)/$', views.DeleteView.as_view(), name='delete_view'),

删除视图将如下所示 views.py

class DeleteView(SuccessMessageMixin, DeleteView):
model = OrderSparePart
success_url = '/'
success_message = "deleted..."

def delete(self, request, *args, **kwargs):
    self.object = self.get_object()
    name = self.object.name
    request.session['name'] = name  # name will be change according to your need
    message = request.session['name'] + ' deleted successfully'
    messages.success(self.request, message)
    return super(DeleteView, self).delete(request, *args, **kwargs)

注意:导入链接中显示的必要导入,或者如果您使用的是pyCharm等IDE,则无需担心它会提示您进行哪些导入。

答案 1 :(得分:3)

这里在fa fa-thrash中传递id和URL,就像我做的那样: -

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

你的网址会像那样儿子:

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