I am attempting to implement quote, buy and sell on the same page, called trade.

I believe it would be a more user friendly way of doing things and good experience.

However, I am having trouble implementing more than one form on the same page.

The quote function works fine, but when I add a separate form for buying, trying to use that form gives me a bad request (400);

Bad Request The browser (or proxy) sent a request that this server could not understand.

I'm not sure why this is happening, but I have a hunch; If one form is called search, and one called buyq, then within my trade function in applications.py I have conditionals designed to flash a message if the user doesn't enter anything into the form, e.g. for search;

    if request.form["search"]=="":

However, I am not sure if request.form returning an empty string is any different from request.form returning nothing at all, in which case posting a request from my buyq form would also trigger the above conditional, since (request.form["search"]=="":) is identical to (not request.form["search"]) and both would be trigger by request.form["buyq"].

Is my hunch correct or am I on the wrong lines?

Here is the relevant code. It only broke when I added the elif bit for buy.


    #if empty form submitted
    if request.form["search"]=="":
        flash("Please enter a stock", "warning")

    #if request was search
    elif request.form["search"]:

        #make sure that stock actually exists

        #else store some variables

    #if request was buy
    elif request.form.get["buyq"]=="":
        flash("Please enter a buy amount", "warning")
        return render_template("trade.html")


    <form action="{{ url_for('trade') }}" method="post">
        <div align="center">
            <div class="input-group">
                <label class="sr-only" for="search">Search</label>
                <input autocomplete="off" autofocus class="form-control" name="search" placeholder="Stock symbol e.g. NFLX" type="text"/>
    {% if session["symbol"] %}

       Stock details

            <div align="center">
                <div class="input-group">
                    <form method="post">
                        <label class="sr-only" for="buyq">Buy</label>
                        <input autocomplete="off" autofocus class="form-control" name="buyq" placeholder="Buy" type="number"/>
    {% endif %}

I have Googled the shit out of this problem but I can't seem to find anything relevant to help me. Anything you could do would be amazing. :)


From the flask doc (The Request Object):

What happens if the key does not exist in the form attribute? In that case a special KeyError is raised. You can catch it like a standard KeyError but if you don’t do that, a HTTP 400 Bad Request error page is shown instead. So for many situations you don’t have to deal with that problem.

buyq does not exist in the request object because it is in a different <form> element. Here's the MDN doc on form.

Sounds like you don't want a different "form", you just want additional input elements in the form, who's keys/values you can examine in the trade route to determine the user's intention.

As to:

I am not sure if request.form returning an empty string is any different from request.form returning nothing at all

No difference.

  • as per usual, you are the man. – Sameer Malik Jun 19 '17 at 14:11
  • sorry for the further questions - but does this mean i need one <form> tag to encapsulate the entire section of code where the forms will be? this seems inelegant when there are elements in that range which are not forms. – Sameer Malik Jun 19 '17 at 16:47
  • Some might argue that having 3 forms when 1 would suffice is inelegant. Flow Content is permitted in a form tag (review the MDN link above). You might be able to accomplish what you want if you add a "action" attribute to all the forms. Oh yeah, you'd need a submit button too. Or maybe <fieldset>. There's probably only a 2 or 3 hundred other ways to design/code this. – DinoCoderSaurus Jun 19 '17 at 18:31
  • okay thank you i'll do more research. – Sameer Malik Jun 19 '17 at 21:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .