0

I have the following view which allows me to save the information of a multi step application.

def saveNewApplication(request, *args, **kwargs):
educationList = [ val for val in pickle.loads(bytes.fromhex(request.session['education'])).values() ]
basicInfoDict = pickle.loads(bytes.fromhex(request.session['basic_info']))
documentsDict = pickle.loads(bytes.fromhex(request.session['documents']))

applicant, created = ApplicantInfo.objects.update_or_create(
    applicantId=request.session['applicantId'],
    defaults={**basicInfoDict}
)
if created:
    #saving the diplomas
    for education in educationList:
        Education.objects.create(applicant=applicant, **education)
    with open(f"{documentsDict['cv_url']}/{request.session['file_name']}", 'rb') as f:
        Documents.objects.create(
            applicant=applicant, 
            cv =File(f, name=os.path.basename(f.name)), 
            langue_de_travail = documentsDict['langue_de_travail']
        )
    #remove the temporary folder
    shutil.rmtree(f"{documentsDict['cv_url']}")
    
else:
    educationFilter = Education.objects.filter(applicant=applicant.id)
    for idx, edu in enumerate(educationFilter):
        Education.objects.filter(pk=edu.pk).update(**educationList[idx])

    #updating the documents
    document = get_object_or_404(Documents, applicant=applicant.id)
    if documentsDict['cv_url']:
        with open(f"{documentsDict['cv_url']}/{request.session['file_name']}", 'rb') as f:
            document.cv = File(f, name=os.path.basename(f.name))
            document.save()
    document.langue_de_travail = documentsDict['langue_de_travail']
    document.save()

languagesDict = pickle.loads(bytes.fromhex(request.session['languages']))
Languages.objects.update_or_create(applicant=applicant, defaults={**languagesDict})
if 'experiences' in request.session and request.session['experiences']:
    experiencesList = [ pickle.loads(bytes.fromhex(val)) for val in request.session['experiences'].values() ]
    Experience.objects.filter(applicant=applicant.id).delete()
    for experience in experiencesList:
        Experience.objects.create(applicant=applicant, **experience)           
return JsonResponse({'success': True})

In the development it works perfectly but if deployed I am getting a 404 raise by this line get_object_or_404(Documents, applicant=applicant.id) meaning the creating is false. and I can't figure why is that. The weirdest thing is if I do comment the entire else block it also returns a 500 error but this time it I do click in the link of the console it show the right response not redirected {success:true} down below is my javascript fonction for handling the view.

applyBtn.addEventListener("click", () => {
  var finalUrl = "/api/applications/save-application/";
  fetch(finalUrl)
  .then(res => res.json())
  .then(data => {
    if (data.success) {
      window.location.href = '/management/dashboard/';
    } else {
      alert("something went wrong, Please try later");
    }
  })
});

I am using postgresql as a database I deleted twice but nothing. the url file is here.

 path("api/applications/save-application/", views.saveNewApplication, name="save-new-application"),
path("api/applications/delete-applicant/<slug:applicantId>/", views.deleteApplicant , name="delete-applicant"),
path('api/edit-personal-info/', editPersonalInfo, name="edit-personal-info"),

Any help or explanation would be highly appreciated. Thanks in advance.

Papis Sahine
  • 105
  • 8
  • It is difficult to reproduce your problem based on your description, you can use failed request tracking to view detailed error information, this will generate detail log file, which will help you to identify the problem. – samwu Dec 05 '22 at 06:22

0 Answers0