user12820114
user12820114

Reputation:

get the value from an html <option> field and get it as a python parameter

I'm trying to get some parameters from this html form but the result is still None. What am I doing wrong?i also tried using url_for () but it didn't work. the passage through url_for occurred with: url_for('controlloEsiti',lingua= lingueDisp,livello=livello)

            Tipologia Studente: <select name="tipoStu">
                <option value="none" selected disabled hidden> Scegli il tipo di studente </option>
                    <option value="studente">Studente</option>
                    <option value="utenteEst">Utente Esterno</option></select><br/><br/>
           
            Lingua: <select name="lingueDisp">
                <option value="none" selected disabled hidden> Scegli la lingua </option>
                    <option value="INGLESE">Inglese</option>
                    <option value="FRANCESE">Francese</option>
                    <option value="SPAGNOLO">Spagnolo</option>
                    <option value="RUSSO">Russo</option>


            </select>   <br/><br/>
            Livello: <select name="livello">
                <option value="none" selected disabled hidden> Scegli il Livello </option>
                    <option value="B1">B1</option>
                    <option value="B1">B2</option>
                    <option value="B1">C1</option>
                    <option value="B1">C2</option>
               </select><br/><br/>
           <input class="btn" type="submit" value="Conferma" id="btncheck"  formaction="/controlloEsiti">

Python:

@app.route('/controlloEsiti',methods=['POST','GET'])
    def controlloEsiti():
        """Funzione per renderizzare la pagina dell'inserimento Esiti"""
        utente = request.args.get("tipoStu")
        lingua= request.args.get("lingueDisp")
        livello=request.args.get("livello")
        
        if utente is None:
            return render_template('caricamentoEsiti.html',login=loginStr(),utente=2,view=0)
        if utente== 'studente':
            call=request.args['call']
            if call=='1':
                matricola=request.args['mat']
                informatizzato=app.model.getEsitiInformatizzatoPerInserimento('', matricola, lingua, livello)
                return render_template('caricamentoEsiti.html',login=loginStr(),informatizzato=informatizzato,view=1)
            else: 
                return render_template('caricamentoEsiti.html',login=loginStr(),utente=0,view=1)
        else:
            call=request.args['call']
            if call=='1':
                email=request.args['email']
                informatizzato=app.model.getEsitiInformatizzatoPerInserimento(email,'', lingua, livello)
                return render_template('caricamentoEsiti.html',login=loginStr(),informatizzato=informatizzato,view=1)
            else:
return render_template('caricamentoEsiti.html',login=loginStr(),utente=-1,view=1)

Upvotes: 0

Views: 147

Answers (1)

Detlef
Detlef

Reputation: 8552

Use url parameters in GET requests and the form object for POST requests.

HTTP GET:

<form method="get">
  <select name="lingueDisp">
    <option value="none" selected disabled hidden>Scegli la lingua</option>
    <option value="INGLESE">Inglese</option>
    <option value="FRANCESE">Francese</option>
    <option value="SPAGNOLO">Spagnolo</option>
    <option value="RUSSO">Russo</option>
  </select>
  <button type="submit">Submit</button>
</form>
@app.route('/demo_get', methods=['GET'])
def demo_get():
    lang = request.args.get('lingueDisp', 'ITALIANO')
    print(lang)
    return render_template('demo_get.html')

HTTP POST:

<form method='post'>
  <select name="lingueDisp">
    <option value="none" selected disabled hidden> Scegli la lingua </option>
    <option value="INGLESE">Inglese</option>
    <option value="FRANCESE">Francese</option>
    <option value="SPAGNOLO">Spagnolo</option>
    <option value="RUSSO">Russo</option>
  </select>
  <button type="submit">Submit</button>
</form>
@app.route('/demo_post', methods=['GET', 'POST'])
def demo_post():
    if request.method == 'POST':
        lang = request.form.get('lingueDisp', 'ITALIANO')
        print(lang)
    return render_template('demo_post.html')

Upvotes: 1

Related Questions