1

As the last requirement of the pset, I want to try empower users to buy more shares or sell shares of stocks they already own via index itself, without having to type stocks' symbols manually. I have already add an button, but i have no idea how to link the button to the corresponding stock symbol within the for loop. And in what way the button can send information back to application.py Any advice would be appreciate, thanks.

{% extends "layout.html" %}

{% block title %}
    Index
{% endblock %}

{% block main %}
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Symbol</th>
                <th>Name</th>
                <th>Shares</th>
                <th>Price</th>
                <th>Values</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        {% for stock in stocks %}
            <tr>
                <td>{{ stock.symbol }}</td>
                <td>{{ stock.stock_name }}</td>
                <td>{{ stock.shares }}</td>
                <td>{{ stock.price }}</td>
                <td>{{ stock.mkt_values }}</td>
                <td><a href="{{ url_for('buy') }}" class="myButton" >BUY</a> <a href="{{ url_for('sell') }}" class="myButton">SELL</a></td>
            </tr>
        {% endfor %}
            <tr> 
                <td colspan="4">CASH</td>
                <td>{{ cash }}</td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="4">Total Asset</td>
                <td style="font-weight: bold;">{{ net_asset }}</td>
            </tr>
        </tfoot>
    </table>
{% endblock %}

enter image description here

3

You can send data back to application.py with a GET method. url_for takes arguments that are sent as a query string. From the flask doc:

flask.url_for(endpoint, **values)

Generates a URL to the given endpoint with the method provided.

Variable arguments that are unknown to the target endpoint are appended to the generated URL as query arguments.

So something like href="{{ url_for('buy',symbol=stock.symbol) }}". Then you can access via request.method.get("symbol") in application.py. There's a flaw with this however. How will you send the number of shares? That is not known when you build the html.

Another approach would be to use a POST method. (Generally speaking use a POST method because you will be doing an update). Here's an overview of how to accomplish that:

  • Make index a real "form". Use login.html as your guide

  • add an input element to allow user to enter number of shares. Make sure you use a "name" attribute; that is what is submitted with the form. Example: input type="number" name="{{stock.symbol}}"

  • make the buttons real button elements, type="submit". They can have different formaction attributes as per this MDN doc. Example: <button type="submit" formaction="{{ url_for('buy') }}" formmethod="post">

  • In the buy||sell route you will need to "analyze" the request.form data. There will be a "key" for each symbol in the list. The one with a "value" (ie not '') will be the number entered by user. It is a string.

4
  • I had the same question. I'm happy to use the GET method (I'm happy for the user to enter the no. of shares they want to buy once they've been served buy.html). But I do want to send the relevant stock symbol back to the buy() method in application.py. I have this in index.html: <button class="btn btn-default" type="submit"><a href="{{ url_for('buy', symbol ='{{share.symbol}}')}}">Buy</a></button>. And this in the buy() method in application.py: if request.method.get("symbol"): symbol = request.method.get("symbol"). I get error AttributeError: 'str' object has no attribute 'get'. Any thoughts? – Matt Friend Jun 19 '17 at 15:41
  • request.method is a string, either GET or POST. Maybe you meant request.form. (And maybe that's what I meant in the answer before recent correction:) – DinoCoderSaurus Jun 19 '17 at 21:01
  • Hmm. As I understand it, the following code in index.html passes back an argument named "symbol", with a value of share.symbol (which for each row in my index table, should be that share's ticker symbol: <button class="btn btn-default" type="submit"><a href="{{ url_for('buy', symbol ='{{share.symbol}}')}}">Buy</a></button>. When I try to access it in application.py: temp_symbol = request.form.get("symbol") if temp_symbol is not None: [FOLLOW PATH A] else: [FOLLOW PATH B] I always end up on path B - I don't seem to access symbol from my button. Thoughts? Also, should I open a new q? – Matt Friend Jun 20 '17 at 11:49
  • Yes, new question – DinoCoderSaurus Jun 20 '17 at 12:41

You must log in to answer this question.

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