Django:: Cant update form

Solution for Django:: Cant update form
is Given Below:

I have gone blind trying to figure this out…

I have done everything correctly but for some reason I am unable to update a form.

url.py

urlpatterns = [
    path('Auditor/', views.Auditor, name="Auditor"),
    path('Auditor/<str:pk>/', views.auditFormPage, name="AuditForm"),
    path('register/', views.registerPage, name="Register"),
    path('Login/', views.loginPage, name="Login"),
    

]

urlpatterns += staticfiles_urlpatterns()

views.py

def Auditor(request):
    model = datas.objects.filter(qs_login='nicobari')
    context = {'items': model }
    return render(request,"main/auditform.html",context)


def auditFormPage(request, pk):
    model = datas.objects.filter(qs_login='nicobari')
    data= datas.objects.get(Task_ID=pk)
    
    form = auditForm(instance=data)

    if request.method == 'POST':
        form = auditForm(request.POST, instance=data)
        if form.is_valid():
            form.save()

    context = {
    "items": model,
    "data": data
    }   

    return render(request,"main/auditform.html", context)

form.py:

class auditForm(ModelForm):
    class Meta:
        model = datas
        fields="__all__"

Everything works but when i try to click on :

<input class="submit" type="submit" value="Submit">

It just refreshes the page but doesnt update the content.

Could anyone help me understand what I could have done wrong?

Models.py

class datas(models.Model):

    country = models.CharField(_('country'),max_length=200,default="Null") 
    qs_login = models.CharField(_('qs_login'),max_length=200,default="Null") 
    Status = models.CharField(_('Status'),max_length=200,default="Null") 
    seller_id = models.CharField(_('seller_id'),max_length=200,default="Null") 
    Task_ID = models.CharField(_('Task_ID'),max_length=200,default="Null",primary_key=True) 
    associate_queue = models.CharField(_('associate_queue'),max_length=200,default="Null") 
    associate = models.CharField(_('associate'),max_length=200,default="Null") 
    metric_wk_no = models.CharField(_('associate'),max_length=200,default="Null") 
    associate_action = models.CharField(_('associate_action'),max_length=200,default="Null") 
    correct_associate_action = models.CharField(_('correct associate action'),max_length=200,default="Null") 
    Associate_Key_Driver = models.CharField(_('Associate_Key_Driver'),max_length=200,default="Null") 
    Sub_Key_driver = models.CharField(_('country'),max_length=200,default="Null") 
    Defect_Area_Associate = models.CharField(_('Defect Area Associate'),max_length=200,default="Null") 
    QA_Comments_on_Associate_Action = models.CharField(_('country'),max_length=400,default="Null") 
    Metric_name = models.CharField(_('Metric name'),max_length=200,default="Null") 


    #SIV
    investigator_task = models.CharField(_('investigator_task'),max_length=200,default="Null") 
    investigator_queue = models.CharField(_('investigator_queue'),max_length=200,default="Null") 
    investigator = models.CharField(_('investigator'),max_length=200,default="Null") 
    verification_action = models.CharField(_('verification_action'),max_length=200,default="Null") 
    correct_investigator_action = models.CharField(_('correct investigator action'),max_length=200,default="Null") 
    Investigator_Key_Driver = models.CharField(_('Investigator Key-Driver'),max_length=200,default="Null") 
    Defect_Area_Investigator = models.CharField(_('Defect Area Investigator'),max_length=200,default="Null") 
    QA_Comments_on_investigator_Action = models.CharField(_('QA Comments on investigator Action'),max_length=200,default="Null") 
    General_Notes = models.CharField(_('General_Notes'),max_length=200,default="Null") 
    
    Action_correctly_captured = models.CharField(_('Action_correctly_captured'),max_length=200,default="Null") 
    Audit_outcome = models.CharField(_('Audit_outcome'),max_length=200,default="Null") 
    associate_resolve_date = models.CharField(_('associate_resolve_date'),max_length=200,default="Null") 
    Type_of_audit = models.CharField(_('Type_of_audit'),max_length=200,default="Null") 
    If_data_correctly_captured= models.CharField(_('If_data_correctly_captured'),max_length=200,default="Null") 

    def __str__(self):
        return f"File: {self.country}-{self.qs_login}-{self.Status}-{self.seller_id}-{self.Task_ID}-{self.associate_queue}-{self.associate}-{self.metric_wk_no}-{self.associate_action}-{self.correct_associate_action}-{self.Associate_Key_Driver}-{self.Sub_Key_driver}-{self.Defect_Area_Associate}-{self.QA_Comments_on_Associate_Action}-{self.Metric_name}-{self.investigator_task}-{self.investigator_queue}-{self.investigator}-{self.verification_action}-{self.correct_investigator_action}-{self.Investigator_Key_Driver}-{self.Defect_Area_Investigator}-{self.QA_Comments_on_investigator_Action}-{self.General_Notes}-{self.Type_of_audit}-{self.Action_correctly_captured}-{self.Audit_outcome}-{self.associate_resolve_date}-{self.If_data_correctly_captured}"

try this

def auditFormPage(request, pk):
    model = datas.objects.filter(qs_login='nicobari')
    data= datas.objects.get(Task_ID=pk)
    
    form = auditForm(instance=data)

    if request.method == 'POST':
        form = auditForm(request.POST, instance=data)
        if form.is_valid():
            form.save()
    else:
        form = auditForm(instance=data)
        context = {
        "items": model,
        "data": data
        }   
    return render(request,"main/auditform.html", context)

Given that the form is valid (please check if it is) and you got to the point where you saved data, you are still pointing to the old snapshot of data when you pass the context to the template. So if you refresh the page again, you should see the updated changes.

What you can do is if it’s a POST request, get the new data that was updated before passing it back to render context:

def auditFormPage(request, pk):
    model = datas.objects.filter(qs_login='nicobari')
    data= datas.objects.get(Task_ID=pk)
    
    form = auditForm(instance=data)

    if request.method == 'POST':
        form = auditForm(request.POST, instance=data)
        if form.is_valid():
            data = form.save() # <-- Add this to get the updated data instance

    context = {
        "items": model,
        "data": data,
    }   

    return render(request,"main/auditform.html", context)