Trouble with redirect(url_for) in flask

19 views Asked by At

I have this code

@views.route("/new_client", methods=['GET', 'POST'])
@login_required
def new_client():
    
    client_id = Cliente.query.order_by(db.desc(Cliente.mod_time)).first().id + 1
    
    iRm = 0.003659

    if request.method == 'POST':
        data = request.form.to_dict()
        
        #Informações pessoais
        mod_time = datetime.now()
        client_name = data['fName']
        cpf = data['cpf']
        client_bday = data['birthday']
        civilState = data['civilState']
        email = data['inputEmail4']
        phone = data['phone']
        address = data['address']
        occupation = data['occupation']
        workplace = data['workplace']
        investProf = data['investProf']
        txtboxes = list()
        client_bday = datetime.strptime(client_bday,'%Y-%m-%d')
        
        for i in range(1,18):
            txtboxes.append(data[f'tbox{i}'])
        cliente = Cliente(mod_time=mod_time, name=client_name, cpf=cpf, birthday=client_bday,
                          civil_state=civilState, email=email,phone=phone,address=address,
                          occupation=occupation, workplace=workplace, investor_profile= investProf,
                          txt_dict_1=txtboxes[0], txt_dict_2=txtboxes[1], txt_dict_3=txtboxes[2],
                          txt_dict_4=txtboxes[3], txt_dict_5=txtboxes[4], txt_dict_6=txtboxes[5], 
                          txt_dict_7=txtboxes[6], txt_dict_8=txtboxes[7], txt_dict_9=txtboxes[8], 
                          txt_dict_10=txtboxes[9], txt_dict_11=txtboxes[10], txt_dict_12=txtboxes[11], 
                          txt_dict_13=txtboxes[12], txt_dict_14=txtboxes[13], txt_dict_15=txtboxes[14], 
                          txt_dict_16=txtboxes[15], txt_dict_17=txtboxes[16], user_id=current_user.id)
        
        if len(client_name) == 0:
            flash('Insira o nome do cliente.', category='error')
        
        elif len(str(client_bday)) == 0:
            flash('Insira a data de nascimento', category='error')
        
        else: 
            db.session.add(cliente)
        
        "...lots of code..."

        else:
            db.session.add(investment)

            db.session.commit()

            return redirect(url_for('views.graphs', id = cliente.id))
        
    return render_template("new_client.html", user=current_user, rate=iRm, id = client_id)

it should redirect to this route below:

@views.route("/report/<int:id>", methods=['GET', 'POST'])
@login_required
def graphs(id):
    
    "...graph plot code..."

    return render_template('reports.html', user=current_user, objGraphs=graphs)

but when i click the submit button in the new_client page, i receive the success message in the terminal "127.0.0.1 - - [26/May/2023 11:51:46] "GET /report/16 HTTP/1.1" 200 -", but the page stay still and doesn't redirect.

what am i doing wrong?

0

There are 0 answers