Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Retaining variables from a text box.

Ramone

Coder
My issue is this. I can retrieve the chosenclass variable from the html text box, and the classroom of students is displayed, but after the updatepa function is called, that is after a student record has been updated, the variable is reset to the initial value of " " and so no class is displayed in the form.

I need a way of retaining the value of chosenclass. I'm thinking flask g or session might work but I'm not really sure how to implement this in my code. Or is there a better way of retaining a variable?

Python:
@app.route('/' , methods = ["GET", "POST"])
def Index():
        chosenclass = request.values.get('chosenclass')
        all_data = Data.query.filter_by(classroom=chosenclass).all()
        return render_template("index.html", students = all_data)

Python:
@app.route('/updatepa', methods = ['GET', 'POST'])

def updatepa():
    if request.method == 'POST':
        my_data = Data.query.get(request.form.get('id'))
        my_data.name = request.form['name']
        my_data.studentid = request.form['studentid']
        my_data.classroom = request.form['classroom']
        my_data.classtime = request.form['classtime']
        my_data.pacomment = request.form['pacomment']

        db.session.commit()
        flash("Student Updated Successfully")
        return redirect(url_for('Index'))

I have tried chosenclass = request.g.get('chosenclass') but receive the error message AttributeError: 'Request' object has no attribute 'g'

Any thoughts on how to go about this would be appreciated
Thanks
Ramone
 
Last edited by a moderator:
Thanks Malcolm. I'll remember to enclose the code in </> in future. In the meantime, I'll keep chipping away at it and experiment with flask g or sessions in a smaller project. But I am still open to any ideas on the best way to approach this issue.
 
Python:
x = "awesome"

def myfunc()
  global x
  x = "fantastic"
  print("Python is " + x)

myfunc()

print("Python is " + x)
This is the answer to your question from w3schools - an incredible resource for learning many programming languages.

You need to declare a variable outside of the function scope first, as you can see above. The word 'global' is used to show that the variable being used inside the function is actually a reference to a variable not given to the function as a parameter.

For example, in almost any programming language you can search "how to use global variable in language?" or "language global variables" will almost always pull up a tutorial or example code for how to access a variable in a function, change it, etc.
 
Last edited:

New Threads

Buy us a coffee!

Back
Top Bottom