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 Return value from HTML select to python app

Ramone

Coder
Hi
I need some help with getting the chosenclass value from my HTML select into my python flask SQLAlchemy data query. I'm not sure what to put in the form action to make it return the chosenclass value to app.py.
Your expertise would be appreciated. Thanks!

HTML:
<form action="." method="POST">
    <select name="chosenclass" id="chosenclass">
        <option value="107i am">107i am</option>
        <option value="107i pm">107i pm</option>
        </select><br>
        <input type="submit" value="Submit">
</form>

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

def Index():
    all_data = Data.query.filter_by(classroom=chosenclass).all()
    return render_template("index.html", students = all_data)
 
Last edited by a moderator:
Solution
Hi Malcolm. Sure, no problem. I dug deeper on https://docs.sqlalchemy.org/ and stackoverflow and found that to gain access to the chosenclass variable in the html file, I just needed to add the line
chosenclass = request.form.get('chosenclass') to the python def Index():

Someone on stackoverflow had posted...

If you want to retrieve POST data:
first_name = request.form.get("firstname")

If you want to retrieve GET (query string) data:
first_name = request.args.get("firstname")

Or if you don't care/know whether the value is in the query string or in the post data:
first_name = request.values.get("firstname")

I don't like to post on stackoverflow as I have found them to be unfriendly to a noob like...
Hi there, Welcome to Code Forum!

I'm not really familiar with this, however, I believe @Ghost or @Krusty the Senile might be of help.

Try changing the form action value to point towards your python file (app.py). So once the form is submitted it will send the data to that file.
 
I did it! Yeah, it's beer O'clock! Thanks for your help Malcolm! I might be back for more help later. I hope this snippet helps others.

[CODE lang="python" title="app.py"]@app.route('/' , methods = ["GET", "POST"])
def Index():
chosenclass = request.form.get('chosenclass')
all_data = Data.query.filter_by(classroom=chosenclass).all()
return render_template("index.html", students = all_data)
[/CODE]
[CODE lang="html" title="Index.html"]<form action="." method="POST">
<select name="chosenclass" id="chosenclass">
<option value="107i am">107i am</option>
<option value="107i pm">107i pm</option>
</select><br>
<input type="submit" value="Submit">
</form>[/CODE]
 
Last edited by a moderator:
Hi Malcolm. Sure, no problem. I dug deeper on https://docs.sqlalchemy.org/ and stackoverflow and found that to gain access to the chosenclass variable in the html file, I just needed to add the line
chosenclass = request.form.get('chosenclass') to the python def Index():

Someone on stackoverflow had posted...

If you want to retrieve POST data:
first_name = request.form.get("firstname")

If you want to retrieve GET (query string) data:
first_name = request.args.get("firstname")

Or if you don't care/know whether the value is in the query string or in the post data:
first_name = request.values.get("firstname")

I don't like to post on stackoverflow as I have found them to be unfriendly to a noob like me.
I also need to add a checkbox to the index.html with the am pm values that I'll call chooseampm. I expect that'll work something like

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

I'm not sure yet as it's early morning here in Cambodia.

I'm not quite finished yet as after the comment is added in a separate form, it returns an empty class. I'll try to fix that or keep things on one form and add a select option to every row. If I get stuck, which I probably will. I'll create another thread for that.

Thanks again and I hope my explanation benefits someone.
Ramone
 

Attachments

  • form.webp
    form.webp
    14.7 KB · Views: 2
Solution

New Threads

Buy us a coffee!

Back
Top Bottom