Home > Back-end >  Why edit is not working even code is correct
Why edit is not working even code is correct

Time:03-30

when I click on update, it is not updating. I don't know what to do. Everything is working except edit.

views.py:

def addnew(request):  
    if request.method == "POST":  
        form = BookForm(request.POST)  
        if form.is_valid():  
            try:  
                form.save()  
                return redirect('/')  
            except:  
                pass 
    else:  
        form = BookForm()  
    return render(request,'book/index.html',{'form':form})

def index(request):  
    books = Book.objects.all()  
    return render(request,"book/show.html",{'books':books})

def edit(request, id):  
    book = Book.objects.get(id=id)  
    return render(request,'book/edit.html',{'book':book})

def update(request, id):  
    book = Book.objects.get(id=id)  
    form = BookForm(request.POST,instance=book)  
    if form.is_valid():  
        form.save()  
        return redirect('/')
    return render(request,'book/edit.html',{'book': book})

def destroy(request, id):  
    book = Book.objects.get(id=id)  
    book.delete()  
    return redirect("/")

urls.py:

from django.contrib import admin
from django.urls import path
from book import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.index,name='index'),  
    path('addnew',views.addnew),  
    path('edit/<int:id>',views.edit),  
    path('update/<int:id>',views.update),  
    path('delete/<int:id>',views.destroy),
]

templates/books: edit.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>
</head>
<body>
    {% block content %}
    <div >  
   <form method="post"  action="/update/{{ book.id }}">  
     {% csrf_token %}  
    <div >  
   <br>  

    <div >  
    <label ></label>  
    <div >  
    <h3>Update Details</h3>  
    </div>  
     </div>  

    <div >  
    <label >Book Id:</label>  
    <div >  
     <input type="text"  name="id" id="id_id" required maxlength="20" value="{{book.id}}"/>
    </div>  
     </div>  

     <div >  
    <label >Book Name:</label>  
    <div >  
     <input type="text"  name="name" id="id_name" required maxlength="100" value="{{book.book_name}}" />  
    </div>  
     </div>    
 
    <div >  
    <label ></label>  
    <div >  
    <button type="submit" >Update</button>  
    </div>  
     </div>  
    </div>  
   </form>
   {% endblock content %}
  </body>
</html>

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>
</head>
<body>
    {% block content %}
    <div >
           
      <form method="post"  action="/addnew">  
        {% csrf_token %}  
       <div >  
      <br>  
       <div >  
       <label ></label>  
       <div >  
       <!-- <h3>Enter Details</h3>   -->
       </div>  
        </div>  
        
        <div >  
       <label >Book Name:</label>  
       <div >  
         {{ form.book_name }}  
       </div>  
        </div>   
       <div >  
       <label ></label>  
       <div >  
       <button type="submit" >Submit</button>  
       </div>  
        </div>  
       </div>  
      </form>  
     </div>
     {% endblock content %}
  </body>
</html>

show.html:

{% block content %}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>
</head>
<body>
    <body>
 
        <div >
            <div >
                
                <div >
                        <h4>Book Records</h4> <span><a href="/addnew" >Add New Book</a></span>
                        <br>
                        <div >
                <table id="bootstrapdatatable"  width="90%">
                            <thead>
                                <th><input type="checkbox" id="checkall" /></th>
                                <th>ID</th>
                                <th>Book Name</th>
                                <th>Edit</th>
                                <th>Delete</th>
                            </thead>
                <tbody>
                {% for book in books %}  
                    <tr>  
                    <td><input type="checkbox"  /></td>
                    <td>{{ book.id }}</td>  
                    <td>{{ book.book_name }}</td> 
                    <td><a href="/edit/{{book.id}}"><span style="color:brown;" ></span></a></p></td>
                    <td><a href="/delete/{{book.id}}"><span style="color:brown;" ></span></a></p></td>  
                    </tr>  
                {% endfor %} 
                </tbody>
                        
                </table>
                        </div>
                    </div>
                    
        <script>
            $(document).ready(function() {
                $('#bootstrapdatatable').DataTable({     
                    "aLengthMenu": [[3, 5, 10, 25, -1], [3, 5, 10, 25, "All"]],
                    "iDisplayLength": 3
                    } 
                );
            } );
            </script>
        
        </body>    
</body>
</html>
{% endblock content %}

Here, edit is not working. when I click on update, it is not updating. I don't know what to do. Everything is working except edit. please anyone tell me what to do. I have tried several times.

forms.py:

from django import forms
from book.models import Book
class BookForm(forms.ModelForm):  
    class Meta:  
        model = Book
        fields = ['book_name']
        widgets = { 'book_name': forms.TextInput(attrs={ 'class': 'form-control' })}

models.py:

from django.db import models

# Create your models here.

    class Book(models.Model):  
        book_name = models.CharField(max_length=100)
    
        class Meta:  
            db_table = "book"

CodePudding user response:

The template of your form is wrong. Pay attention that your form is waiting for you to send it a field called book_name:

class BookForm(forms.ModelForm):  
    class Meta:  
        model = Book
        fields = ['book_name']
        widgets = { 'book_name': forms.TextInput(attrs={ 'class': 'form-control' })}

However, in the template you are sending it the id field and a field called name:

<input type="text"  name="id" id="id_id" required maxlength="20" value="{{book.id}}"/>
<input type="text"  name="name" id="id_name" required maxlength="100" value="{{book.book_name}}" />

So, according your forms.py, you must to change the template like this:

<form method="post"  action="/update/{{ book.id }}">  
{% csrf_token %}  
<div >  
<br>  
<div >  
  <label ></label>  
  <div >  
    <h3>Update Details</h3>  
  </div>  
</div>  
<div >  
<label >Book Name:</label>  
<div >  
  <input type="text"  name="book_name" id="id_book_name" required maxlength="100" value="{{book.book_name}}" />  
</div>  
 </div>    
<div >  
<label ></label>  
<div >  
<button type="submit" >Update</button>  
</div>  
 </div>  
</div>  

BTW, you could simplify all of this, using the power of Django like this:

<form method="post"  action="/update/{{ book.id }}">
   {% csrf_token %}
   {{ form }}
   <button type="submit" >Update</button>
</form>

You should study the way in witch Django builds forms: https://docs.djangoproject.com/en/dev/topics/forms/

  • Related