2

I'm new to Django and i try my best to resolve this problem. So i try to create a delete button on my template. I create my view and i create my url but it doesn't work.

So here my views :

def book_delete(request, pk):
book = get_object_or_404(Book, pk=pk)
if request.method == 'POST':
    book.delete()
    return redirect('/')
return render(request, "livre/newbook_form.html", {'form': book})

My urls

from . import views
from django.conf.urls import url

urlpatterns = [
        url(r'^livre/(?P<pk>[0-9]+)/$', views.book_delete, name='book_delete')
    ]

My template

<form action="liste" method="POST">{% csrf_token %}
    <table>
        <th>Titre</th><th>Auteur</th><th>Nombre de pages</th><th>Supprimer</th>

            {% for Book in Book %}
            <tr>
                <td>{{ Book.title }}</td>
                <td>{{ Book.author }}</td>
                <td>{{ Book.num_pages }}</td>
                <td>
                    <form action="{% url 'book_delete' Book.id %}" method="POST">
                        {% csrf_token %}
                        <input type="button" value="{{Book.id}}"/>
                    </form>
                </td>
            </tr>
            {% endfor %}
    </table>
</form>
 

I don't know why it doesn't work. Can you help me ?

1 Answer 1

1

You need to submit the form, so with type="submit" [dev-mozilla]:

<form action="{% url 'book_delete' Book.id %}" method="POST">
    {% csrf_token %}
    <input type="submit" value="delete"/>
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot : )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.