Django - How to delete a object directly from a button in a table

后端 未结 2 1375
迷失自我
迷失自我 2021-02-09 21:11

(sorry for my bad english)

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

I have a work orders, that have spare

相关标签:
2条回答
  • 2021-02-09 21:48

    In your html template inside for loop use the form tag inside <td> to create delete button as below (css class will work if you are using bootstrap3):

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

    add urlpattern in urls.py

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

    delete view will be like below in 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)
    

    Note: import necessary imports shown in links or you need not to worry if you are using IDE such as pyCharm it will prompt you which import to make.

    0 讨论(0)
  • 2021-02-09 21:53

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

    in ur 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')
    
    0 讨论(0)
提交回复
热议问题