Trying to skip html form in Flask - whenever I enter data in second form, flask keeps calling first request form

Viewed 29

I am still learning flask and have a project to create a small app which will return some objects via API. I am having trouble with html forms - I cannot skip the first form, API keeps calling it if I leave it empty. To be precise - it does not even reach second form. I will give some code here.

This is html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>API App</h1>
    <section class="forms">
        <form method="post" action="{{ url_for('api_app.get_customer', store_id=store_id) }}">
            <label for="customer_id">/customers/id/</label>
            <input type="text" placeholder="id(text field)" name="customer_id" id="customer_id"/>
            <button type="submit">Run</button>
        </form>
        <form method="post" action="{{ url_for('api_app.get_address', store_id=store_id) }}">
            <label for="address_id">/addresses/id/</label>
            <input type="text" placeholder="id(text field)" name="address_id" id="address_id"/>
</form>
    </section>
    <section class="response">
        <h1>Response</h1>
        <div>
            {% if customer %}
                <div id="result">{{ customer }}</div>
            {% else %}
                JSON
            {% endif %}
        </div>
    </section>
    <script>
        function pretty_print_json(element_id){
            element = document.getElementById(element_id)
            jsonobj = JSON.parse(element.innerHTML)
            pretty_json = JSON.stringify(jsonobj, undefined, 4)
            element.innerHTML = "<pre>" + pretty_json + "</pre>"
        }

        pretty_print_json('result')
    </script>
</body>
</html>

This is my class code which has calling methods:

class ApiCalling:
    def __init__(self, store_id):
        self.api = Api(store_id=store_id)


    def get_customer_object(self, customer_id, store_id):
        customer = self.api.get(endpoint=f"/customers/{customer_id}")
        return customer.json()

    def get_address_object(self, address_id, store_id):
        address = self.api.get(endpoint=f"/addresses/{address_id}")
        return address.json()

Routs code:

@api_app_bp.route("/<int:store_id>", methods=["GET", "POST"])
@admin_required
def get_customer(store_id):
    customer_id = request.form.get("customer_id")
    customer = ApiCalling(store_id).get_customer_object(customer_id, store_id)
    if request.method == "GET":
        return render_template("/api_app/index.html", store_id=store_id)
    return render_template(
        "/api_app/index.html", customer=json.dumps(customer, indent=4, default=str), store_id=store_id
    )

@api_app_bp.route("/<int:store_id>", methods=["GET", "POST"])
@admin_required
def get_address(store_id):
    address_id = request.form.get("customer_id")
    address = ApiCalling(store_id).get_address_object(address_id, store_id)
    if request.method == "GET":
        return render_template("/api_app/index.html", store_id=store_id)
    return render_template(
        "/api_app/index.html", address=json.dumps(address, indent=4, default=str), store_id=store_id
    )

The issue I am getting when running this one is:

404 GET /api/customers/None?

Doesn't even reach to another form or route. Not sure how to validate this and skip the first customer field once I eneter only data in address field? Any advice would be appreciated here. Thank you in advance!

0 Answers
Related