Skip to main content
added 632 characters in body
Source Link

This is my code for quote.html:

{% extends "layout.html" %}

{% block title %}
    Search Stock
{% endblock %}

{% block main %}
    <form action="{{ url_for('quote') }}" method="post">
        <fieldset>
            <div class="form-group">
                <input class="form-control" name="search" placeholder="Search" type="text"/>
            </div>
            <div class="form-group">
                <button class="btn btn-default" type="submit">Search</button>
            </div>
        </fieldset>
    </form>
{% endblock %}

And this is my code in quoted.html:

And this is my code in quoted.html:

This is my code for quote.html:

{% extends "layout.html" %}

{% block title %}
    Search Stock
{% endblock %}

{% block main %}
    <form action="{{ url_for('quote') }}" method="post">
        <fieldset>
            <div class="form-group">
                <input class="form-control" name="search" placeholder="Search" type="text"/>
            </div>
            <div class="form-group">
                <button class="btn btn-default" type="submit">Search</button>
            </div>
        </fieldset>
    </form>
{% endblock %}

And this is my code in quoted.html:

Source Link

Pset 7 quote - help!

I'm having trouble with the quote method. When I go to the quote tab of my CS50 Finance site, it says:

AttributeError: 'NoneType' object has no attribute 'startswith'

Seems as if lookup is returning None automatically - although I'm not sure if I'm correct on this. Could someone help me figure out what I'm doing wrong?

Here is my code in application.py:

@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
    """Get stock quote."""
    
    quote = lookup(request.form.get("search"))
    return render_template("quoted.html", name=quote)

And this is my code in quoted.html:

{% block title %}
    Stock
{% endblock %}

{% block main %}
    <p>
        {{ name }}
    </p>
{% endblock %}

Lastly, here is the lookup method in helpers.py, which I don't think I need to alter:

def lookup(symbol):
    """Look up quote for symbol."""

    # reject symbol if it starts with caret
    if symbol.startswith("^"):
        return None

    # reject symbol if it contains comma
    if "," in symbol:
        return None

    # query Yahoo for quote
    # http://stackoverflow.com/a/21351911
    try:
        url = "http://download.finance.yahoo.com/d/quotes.csv?f=snl1&s={}".format(symbol)
        webpage = urllib.request.urlopen(url)
        datareader = csv.reader(webpage.read().decode("utf-8").splitlines())
        row = next(datareader)
    except:
        return None

    # ensure stock exists
    try:
        price = float(row[2])
    except:
        return None

    # return stock's name (as a str), price (as a float), and (uppercased) symbol (as a str)
    return {
        "name": row[1],
        "price": price,
        "symbol": row[0].upper()
    }

Thank you for helping!