0

When I launch my web server and test "quote" I get an AssertionError after I search for the symbol. No matter what I change or tweak the error is OMNISCIENT. No kidding. The error states:

"AssertionError: A setup function was called after the first request was handled. This usually indicates a bug in the application where a module was not imported and decorators or other functionality was called too late. To fix this make sure to import all your view modules, database models and everything related at a central place before the application starts serving requests."

My code for quote is:

@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():

    if request.method == "POST":

        if not request.form.get("symbol"):
            return apology("Must enter stock symbol")

        symbols = request.form.get("symbol")

        if not lookup(symbols):
            return apology("Stock doesn't exist")

        stock = lookup(symbols)

        name = stock["name"] 
        price = stock["price"] 
        symbol = stock["symbol"]

        @app.route("/stock")

        def stock():

            return render_template("stock.html", name = name, price = price, symbol = symbol)

    else:

        return render_template("quote.html")

It's very confusing because the information provided by the error message doesn't do much to direct me. However the traceback or most recent call, to the program was given as:

@app.route("/stock")

The two templates I'm using are stock.html and quote.html, quote is to get the symbol and stock is to display the information. Quote is:

{% extends "layout.html" %}

{% block title %}
    Quote
{% endblock %}

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

And the code for stock is:

{% extends "layout.html" %}

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

{% block main %}
    <form action="{{ url_for('stocks') }}" method="post">
            <p>
                Name: {{name}}

                Price: {{price}}

                Symbol: {{symbol}}
            </p>
    </form>
{% endblock %}

If anyone could look into (and hopefully help me solve) my problem that would be fantastic. Thanks!

1 Answer 1

1

In flask, @app.route is a decorator. From the flask View Decorators doc:

To use the decorator, apply it as innermost decorator to a view function. When applying further decorators, always remember that the route() decorator is the outermost.

So a route decorator cannot be defined inside another. Don't you simply want to return render_template(.... ?

6
  • Thanks, sorry if its a novice mistake! Jul 17, 2017 at 11:30
  • 1
    That's what learning is, a series of "novice mistakes". Keep learning! Jul 17, 2017 at 11:32
  • If I exclude the decorator it doesn't seem to work, saying "could not build url for endpoint stock". I think I have to include the route, do you think I could do it any other way? Jul 17, 2017 at 11:41
  • 1
    The error is probably coming from here <form action="{{ url_for('stocks') }}" method="post"> if you do not have an app route defined for stocks. Jul 17, 2017 at 12:40
  • 1
    stock.html is looking for a route named "stocks". Did you spell it correctly? Otherwise, you'll need to paste additional code. Jul 19, 2017 at 11:30

You must log in to answer this question.

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