Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

Django only show first POST element in loop

I have simple library page that user can register, add and editdelete records from library.

Template.html

{% extends "book/base.html" %}
{% block content %}

    <div class="row justify-content-center mt-5">
    </div>

 {% if books %}
      
      {% for book in books %}
<div class="table-users">
   <div class="header"> {{ book.title }} </a>   </div>
   <table cellspacing="0">
      <tr><center>
         <th>Name</th>
         <th>Author</th>
         <th>Delete</th>
</center>
      </tr>
         <td> {{ book.name }} </td>
         <td> {{ book.author }} </td>
         <td> <form class="delete" method="POST" action="{% url 'deletebook' book.id %}">  {% csrf_token %}  <button class="delete" type="submit" class="btn btn-warning">Delete</button></td> 

views.py:

def deletebook (request, book_pk):
    book = get_object_or_404(Book, pk=book_pk, user=request.user)
    if request.method == 'POST':
        book.delete()
        return redirect('currentbooks')

With this loop only first element of POST method is active and working. Actually I'd debuggging html after rendered and i've seen that theres a only first element has a POST method. I searched on google and on stackoverflow and found something about the change id to class. But my template doesnt have any id. I also tried to move {% csrf_token %} outside of the loop but it doesnt work either. I think so, i missed something important here. I really appreciate if someone could help me out. Thank you in advance.

question from:https://stackoverflow.com/questions/65943022/django-only-show-first-post-element-in-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...