how to delete an image using django?

前端 未结 2 1963
闹比i
闹比i 2021-01-26 19:07

I want to know how to add a delete button in my form for deleting an image.

The project is a simple \"Pics viewer\", the index shows all the images that are stored but I

2条回答
  •  醉话见心
    2021-01-26 19:51

    views.py

    from django.views.generic.edit import DeleteView
    from django.core.urlresolvers import reverse_lazy
    from myapp.models import Image
    
    class ImageDelete(DeleteView):
        model = Image
        success_url = reverse_lazy('index')
    

    urls.py

    from myapp.views import index, ImageDelete
    
    ...
    url(r'^image/(?P\d+)/delete/$', ImageDelete.as_view(), name="image-delete"),
    ...
    

    index.html

    {% for image in images_list %}
        
  • Delete
  • {% endfor %}

提交回复
热议问题