I am implementing a quote function in application.py. But the result is ridiculous. After I enter a symbol to search for, I get an apology.html(saying Missing Username!), but this apology.html with this message I have to receive only while registering and not entering Username.

Why do I get this .html after executing quote function?? What is the problem?

Thanks ;)

Here is my implementation of quote:

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

if request.method == "POST":
    quote = lookup(request.form.get("symbol"))

    if quote == None:
        return apology("Invalid symbol")

    return render_template("quote_response.html", name = quote["name"], symbol = quote["symbol"], quote = quote["price"])
    return render_template("quote.html")

Here is register function (seems to work correctly)

@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user.*"""

# forget any user_id

if request.method == "POST":

    if not request.form.get("username"):
        return apology("Missing Username!")
    elif not request.form.get("password"): 
        return apology("Missing password!")

    if request.form.get("password") != request.form.get("password_confirm"):
        return apology ("Passwords do not match!")

    #  hash the password
    encrypted = pwd_context.encrypt(request.form.get("password"))

    # username must be uniq
    result = db.execute("INSERT INTO users (username, hash) VALUES (:username, :password)", 
        username = request.form.get("username"), password = encrypted)
    if not result:
        return apology("Username is already exists")

    # store id in session
    rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))

    session["user_id"] = rows[0]["id"]

    # redirect user to home page
    return redirect(url_for("index"))

    return render_template("register.html")

My quote.html

{% extends "layout.html" %}

{% block title %}
{% endblock %}

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

Look at the tab when you are entering the stock symbol. Does it say "Register"? Looks like quote.html started with the register.html code. It's still calling the register route on submit.

  • Yes, exactly. I copied code from register.html, changed form, but did not pay attention to form action. Every time I submitted a form, register function was called. Changed form action form action="{{ url_for('quote') }}" method="post" and everything is fine. Thanks! Jun 18 '17 at 21:48

You must log in to answer this question.

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