Submit Button checks if file doesn't select in Django

Currently working on a Django project where I am stuck in a situation and the scenario is something like that I have two forms in my abc.html page, one is used for input file and second is used to run python script internally. But the issue is even if I don't input the file the submit button "run python script" start's working without submitting a file. Here I want to create a check that submit button "run python script" will run only at one condition when file will submitted otherwise button will disable. It will active only at one condition when user will input the file. I am sharing the details:

abc.html:

<!--form to input file -->
<form method="post" enctype="multipart/form-data" name="myform">
      {% csrf_token %}
                    
<input type="file" id="file" name="doc" class="inputfile" onchange="document.myform.submit()">                 </form>
                          <-- end of input file-->

 <!-- form to run python script -->
<form action = "/results/" method="post" id="subform">
     {% csrf_token %}
<input type="submit" id="btnSubmit" name="doc" value="run python script" class="btn btn-warning btn-sm" />
 </form>
                    <-- end of form running python script -->

views.py:

def compliance_check(request):              //function to upload file
  global uploaded_file
  if request.method == 'POST':
    uploaded_file = request.FILES['doc']
    print(uploaded_file.name)
    print(uploaded_file.size)
    fs = FileSystemStorage()
    fs.save(uploaded_file.name, uploaded_file)
    messages.info(request, 'your file ' + uploaded_file.name + " has been uploaded successfully")
  return render(request, 'enroll/abc.html')

def results(request):                       //function to run python script
  user_id = request.session['user_id']
  
  hash_id, id_exists = compliance(user_id)
  request.session['hash_id'] = hash_id

  if id_exists:
    messages.info(request, "This File has already been analyzed")

  return redirect(tables_view)
 

I have tried multiple ways to create a check but not succeed yet. I hope that everyone will understand the question and the scenario is simple that there should be a check before clicking on run script button that please select the file first. The button should run only at one condition if user upload file.

Back to Top