Django: How can I call a view function from template?

后端 未结 6 1948
难免孤独
难免孤独 2020-11-27 12:27

I have a question on how to call a view function from a template HTML button? Like an onclick function? Here is the template:



        
6条回答
  •  一向
    一向 (楼主)
    2020-11-27 13:17

    For deleting all data:

    HTML FILE

      class="btn btn-primary" href="{% url 'delete_product'%}">Delete
    

    Put the above code in an anchor tag. (the a tag!)

    url.py

    path('delete_product', views.delete_product, name='delete_product')]
    

    views.py

    def delete_product(request):
        if request.method == "GET":
            dest = Racket.objects.all()
            dest.delete()
            return render(request, "admin_page.html")
    

提交回复
热议问题