php - How to implement an external python code to a Django web server?

as from the title, I am trying to implement an external python code to a Django web server.
I am quite new to programming so any hints will be surely helpful.

Long story short: I am trying to set up a form where the user have to insert an aminoacidic sequence. This sequence should pass to my python script, that is able to compare it with all the sequences already present in the database and gives as a result the most similar ones. My problem is that I am not able to let my form and my script talk each others.
I have followed the Django documentation here https://docs.djangoproject.com/en/3.2/topics/forms/ but this did't helped too much.
Also roaming online and browse already asked questions here was unfruitful.
Please find here below the files:
BLAST_page.html (tried both, commented and uncommented)

{% extends "base_generic.html" %}

{% block content %}
<div class="container-fluid" style="text-align: center;" ></div>
    <form method="post" action= {% url 'BLAST-process' %}>
        {% csrf_token %}
        {{ blast }}
        <label for="sequence">Type or paste your sequence in the box below</label><br><br> 
        <input type="text" id="sequence" class="input_text" name="sequence" value="{{ sequence }}" style="width:600px; height:200px;"><br><br>
        <input type="submit" value="Submit">
    </form>
</div>
    {% endblock %}    
<!--    
    <div class="container-fluid" style="text-align: center;" >
    <form method="POST" action= {% url 'BLAST-process' %}>
        {% csrf_token %}
    <label for="sequence">Type or paste your sequence in the box below</label><br><br>  
    <input type="text" id="sequence" class="input_text" name="sequence" value="{{ sequence }}" style="width:600px; height:200px;"><br><br>  
    <input type="submit" value="Submit">
    </form>
</div>
-->

In order to check if this form works, I have used this simple .php script. The reasoning behind is that if the form works correctly, the inserted data should be echoed. But this doesn't happen.

<html>
<body>

Sequence: <?php echo $_POST["sequence"]; ?><br>
<?php
    echo "<h2>Your Input:</h2>";
    echo $sequence;
    ?>
</body>
</html>

forms.py

from django import forms

class blast(forms.Form):
    sequence = forms.CharField(help_text="Enter a sequence", label='sequence')

blast.py the script that should receive data from the form

from Bio.Blast.Applications import NcbiblastpCommandline
from io import StringIO
from Bio.Blast import NCBIXML
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio import SeqIO
import numpy as np

# Create two sequence files

#taken from the textArea.
sequence = sequence
seq1 = SeqRecord(Seq(sequence), id="x")

SeqIO.write(seq1, "seq1.fasta", "fasta")
#SeqIO.write(seq2, "seq2.fasta", "fasta")

# Run BLAST and parse the output as XML
output = NcbiblastpCommandline(query="seq1.fasta", 
                               subject="/Users/santarpia/Documents/tutorial/codorenv/RepOdor/FASTA/db.fasta",
                               outfmt=5)()[0]

blast_result_record = NCBIXML.read(StringIO(output))

# Print some information on the result
for alignment in blast_result_record.alignments:
    for hsp in alignment.hsps:
        print('***Alignment****\n')
        print('Alignment title', alignment.title)
        print('Alignment Length:', alignment.length)
        print('E-value:', hsp.expect)
        print('Gaps:', hsp.gaps)
        print('Identities:', hsp.identities)
        print('Positives:', hsp.positives)
        print('Score:', hsp.score)
        print('Bits:', hsp.bits)
        print('Coverage:', hsp.align_length)
        print('% Identity:', np.round((hsp.identities / hsp.align_length) * 100, 2))
        print("\n")
        print (hsp.query[0:])
        print("\n")
        print (hsp.match[0:])
        print("\n")
        print (hsp.sbjct[0:])
        print('****************************\n\n\n')

As said, any comment on how to set up this would be highly appreciated. If you need more files or information to answer to my question, feel free to ask for them.

Answer

Solution:

So if you followed the documentation correctly after submitting the sequence to the form the code should "enter" the if request.method == 'POST' portion of the view. You can verify this by putting a print("hello world") statement under the if (or an import pdb; pdb.set_trace()). Once there, you can get the sequence from the form with sequence = form.cleaned_data['sequence']. Now to pass it to your script you need your script to be a method that can take in an input (the sequence) so wrap you script in something like def findMostSimilarSequence(sequence): and remove that first line sequence = sequence then in your view you can import the method and call it with the sequence varible from your form.

Source