Monday, March 22, 2021

Edit record is not saving changes in frontend

I am creating a CRUD application. The create, read and delete function are working but edit record is showing the instance in the form but it is not saving changes.

Here is my code.
=======
views.py

def edit(request, id):
    student = Student.objects.get(id=id)  
    form = StudentForm(request.POST, instance = student) 
    if form.is_valid():  
        form.save()  
        return redirect("/")      
    return render(request, 'pages/edit.html', {'form': form, 'student': student}) 


urls
edit.html

{% extends 'pages/base.html' %}

{% block content %}

    <form method="POST" action="" enctype="multipart/form-data">
        {% csrf_token %}
        <input type="hidden" name="id" id="id" value="{{student.id}}"><br><br>
        
        <label for="">First Name</label>
       <input type="text" maxlength="20" name="first_name" id="first_name" value="{{student.first_name}}"><br><br>

        <label for="">Last Name</label>
        <input type="text" maxlength="20" name="last_name" id="last_name" value="{{student.last_name}}"><br><br>
       
        <label for="">Date of birth</label>
        <input type="text" maxlength="20" name="date_of_birth" id="date_of_birth" value="{{student.date_of_birth}}"><br><br>
       
        <label for="">level</label>
        <input type="text" maxlength="20" name="level" id="level" value="{{student.level}}"><br><br>
        
     <label for="">Gender</label>
     <!-- <input type="text" name="gender" id="gender" value="{{student.gender}}"><br><br> -->
           <Select name="gender">
            <option>Select</option>
            <option value="Male">Male</option>
            <option value="Female">Female</option>
        </Select><br><br>
     
        <label for="">Phone</label>
        <input type="text" maxlength="20" name="phone" id="phone" value="{{student.phone}}"><br><br>

        <label for="">Department</label>
        <input type="text" maxlength="20" name="department" id="department" value="{{student.department}}"><br><br>
 
        <label for="">Email</label>
        <input type="text" maxlength="20" name="email" id="email" value="{{student.email}}"><br><br>
 
      
       <label for="">Address</label>
       <input type="text" maxlength="20" name="addressl" id="address" value="{{student.address}}"><br><br>
     

       <label for="">Course</label>
       <!-- <input type="text" name="course" id="course" value="{{student.course}}"><br><br>
       -->
        {% for i in student.course.all %}
        <input type="text" value="{{i.name}}"><br><br>
        {% endfor %}
       
        <input type="submit" value="Submit">
    </form>
{% endblock %}


What could be wrong?
Thank you in advance.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b6a1b3ba-679d-448d-83b5-1aaedd7af30an%40googlegroups.com.

No comments:

Post a Comment