0

I have a Flask backend with MySQL and need to pull values from a table to display in a dropdown.

I have a table with single values like Banana, Apple, Orange, etc.

Inside my html code, the option shows up if a user selects another dropdown (which is hard-coded) so the code looks like this:

$(function() {
        $("#inputType").change(function() {
            var myform = "<div id=\"newForm\">"
            +"<label for=\"inputFruit\">Fruit:</label>"
            +"<select class=\"form-control\" id=\"inputFruit\" required>"
            +"<option></option>"
            +"</select>"
            if ($("#inputType").val() == "fruits") {
                $(myform).insertBefore("#btnSignUp");
            } else {
                $("#newForm").remove();
            }

Inside my flask app:

@app.route('/getFruits') # gets my fruits as a list and returns

Where do I add code so that the values I get from /getFruits is put into the option in the HTML?

0

1 Answer 1

1

You can pass the context data (fruits list) into javascript code of your template, use JSON will meet your requirement:

Your View:

import json

@app.route('/getFruits')
def my_view():
    data = ['Banana', 'Apple', 'Orange', 'Pear', 'Watermalon'] # you can get list from your DB instead
    return render_template('yourtemplate.html', data=json.dumps(data))

Your Template:

$(function() {
        $("#inputType").change(function() {
            var myform = "<div id=\"newForm\">"
            +"<label for=\"inputFruit\">Fruit:</label>"
            +"<select class=\"form-control\" id=\"inputFruit\" required>"
            {% for item in data %}
            +"<option>{{ item|safe }}</option>"
            {% endfor %}
            +"</select>"
            if ($("#inputType").val() == "fruits") {
                $(myform).insertBefore("#btnSignUp");
            } else {
                $("#newForm").remove();
            }
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for this, what if I have two dropdowns? If I had a data1 and data2 inside of my_view?
@user1883614 It will be similar, just add one more variable, and pass the variable in the render_template('yourtemplate.html', data1=json.dumps(data1), data2=json.dumps(data2)), after that in your template, you can for loop data1 and data2 separately, and assign the variables into two dropdown Correspondingly.
Basically, you can render a template with the given context (the variables that should be available in the context of the template), please refer to Template Rendering. You can pass multiple variables to template, just make sure your variable can be "understood" by javascript.
@Tiny.D, you should not be using json with Flask, instead use jsonify which is provided by Flask itself, more secure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.