'Python: Create a Main Menu page using flask
I have the following code:
@app.route('/mainMenu', methods=['GET', 'POST'])
def mainMenu():
def name():
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
sqlFile = open('name.sql')
sqlCode = sqlFile.read()
cursor.execute(sqlCode, (session['name'],))
user = cursor.fetchone()
if request.method == 'GET':
name = request.form["name"]
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
return render_template('mainMenu.html', user=user)
return redirect(url_for('mainMenu'))
def age():
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
sqlFile = open('age.sql')
sqlCode = sqlFile.read()
cursor.execute(sqlCode, (session['age']))
user = cursor.fetchone()
if request.method == 'GET':
age = request.form["age"]
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
return render_template('mainMenu.html', user=user)
return redirect(url_for('mainMenu'))
def location():
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
sqlFile = open('location.sql')
sqlCode = sqlFile.read()
cursor.execute(sqlCode, (session['location']))
user = cursor.fetchone()
if request.method == 'GET':
location = request.form["location"]
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
return render_template('mainMenu.html', user=user)
return redirect(url_for('mainMenu'))
def height():
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
sqlFile = open('height.sql')
sqlCode = sqlFile.read()
cursor.execute(sqlCode, (session['height']))
user = cursor.fetchone()
if request.method == 'GET':
height = request.form["height"]
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
return render_template('mainMenu.html', user=user)
return redirect(url_for('mainMenu'))
return redirect(url_for('mainMenu'))`
What I am trying to do is create a Main Menu page that displays Name, Age, Location, Height of each user when they login. The above code gives me this error:
Too many redirects occurred to open "127.0.0.1:5000/mainMenu". This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.
How would I fix this?
Solution 1:[1]
I think you need to have one page (main menu) that calls each function and returns the value for each (name, age, height and location) and then renders the template you want, so you should only have one render template or redirect at in there after the functions are called, I hope that makes sense.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Aristotle |
