-1

i have a django website where it include an update function that filters the data based on the selected ID and returns all the related values of the requested record, where the user is allow to edit data and fields can't be empty.

the update is done only if all the fields are edited

my question is how to do the update even if just one field is edited

views.py

def update(request,pk):
    #deny anonymouse user to enter the  create page
    if not request.user.is_authenticated:
        return redirect("login")
    else:
        dbEntry = suspect.objects.get(pk =pk)
        print( "db entry : ",dbEntry)



        if request.method == 'POST': 
            first_name = request.POST['fname']
            last_name = request.POST['lname']
            fatherName = request.POST['fatherName']
            motherName = request.POST['motherName']
            gender = request.POST['gender']
            content = request.POST['content']

            dbEntry = suspect.objects.filter(pk = pk).first()
            if dbEntry:
                dbEntry.suspect_name = first_name
                dbEntry.suspect_last_name = last_name
                dbEntry.suspect_father_name = fatherName
                dbEntry.suspect_mother_name = motherName
                dbEntry.gender = gender
                dbEntry.content = content
                dbEntry.save()
                return redirect("list")
                print( "db entry after update: ",dbEntry)
            else:
                raise Http404('Id not found')

                return render(request,'blog/update.html', {"dbEntry":dbEntry})

update.html

 {% extends "base.html" %}
    {% load static %}
    {% block body %}


    <head>
        <link rel="stylesheet" type="text/css" href="{% static '/css/linesAnimation.css' %}">
        <link rel="stylesheet" type="text/css" href="{% static '/css/input-lineBorderBlue.css' %}">
        <link rel="stylesheet" type="text/css" href="{% static '/css/dropDown.css' %}">
        <link rel="stylesheet" type="text/css" href="{% static '/css/home.css' %}">
        <link rel="stylesheet" type="text/css" href="{% static '/css/meta-Input.css' %}">
        <meta name= "viewport" content="width=device-width, initial-scale=1.0">
        <script type="text/javascript" src="{% static '/js/jquery-3.1.1.min.js'%}"></script>
        <title>Welcome</title>
    </head>
    <body>


        <div class="lines">
      <div class="line"></div><div class="line"></div>
      <div class="line"></div><div class="line"></div>
      <div class="line"></div><div class="line"></div><div class="line"></div>
      </div>
        <form  method  = "POST" enctype="multipart/form-data">
            {% csrf_token %}
        <div id='left-column-Input' class="formInput" include="select()"> 
            <div class="forminputs">
            <input type="text" id="fname" name="fname" autocomplete="off" required />
            <label for="fname" class="label-name">
                <span class="content-name" name="fname">{{dbEntry.suspect_name}}</span>
            </label></div>
<div class="forminputs">
        <input type="text" id="lname" name="lname" autocomplete="off" required />
        <label for="lname" class="label-name">
            <span class="content-name" name="lname">{{dbEntry.suspect_last_name}}</span>
        </label></div>

    <div class="forminputs">
        <input type="text" id="fatherName" name="fatherName" autocomplete="off" required />
        <label for="fatherName" class="label-name">
            <span class="content-name" name="fatherName">{{dbEntry.suspect_father_name}}</span>
        </label></div>

    <div class="forminputs">
        <input type="text" id="motherName" name="motherName" autocomplete="off" required />
        <label for="motherName" class="label-name"> 
            <span class="content-name" name="motherName">{{dbEntry.suspect_mother_name}}</span>
        </label></div>
    <div class="home-Button">
        <button id="save" name="save" type="submit">Edit</button>
    </div>
jottbe
  • 4,228
  • 1
  • 15
  • 31
Django Dg
  • 97
  • 4
  • 19
  • Why not pass your instance to your form like this `form = YOURFORM(request.POST or None, instance=EXISTING_INSTANCE)`? You can read up on that [here i.e.](https://stackoverflow.com/questions/1854237/django-edit-form-based-on-add-form). – creyD Jul 23 '19 at 07:13
  • form = YOURFORM() you mean django model form ?? i faced a problem with styling this forms otherwise its much better to use the model form in order to perform the CRUD functions – Django Dg Jul 23 '19 at 07:18

1 Answers1

0

Provide the input value in your template like this:

<div id='left-column-Input' class="formInput" include="select()"> 
  <div class="forminputs">
  <input type="text" id="fname" name="fname"  value = "{{dbEntry.suspect_name}}" autocomplete="off" required />
    <label for="fname" class="label-name">
    <span class="content-name" name="fname">{{dbEntry.suspect_name}}</span>
    </label></div>
arjun
  • 7,230
  • 4
  • 12
  • 29