0

I use four arguments in quoted.html, but when I try to pass them to from my application.py, there is an error: TypeError: render_template() takes 1 positional argument but 4 were given

@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
    """Get stock quote."""
    
    #Look up for the stock symbol when user requires
    if request.method == "POST":
        
        #Check the symbol is none
        if lookup(request.form.get("symbol")) == None:
            return apology("The company's stock is not found!", 403)
        
        #Show the stock result including name of the company and the stock price 
        else:
            name = lookup(request.form.get("symbol"))["name"]
            symbol = lookup(request.form.get("symbol"))["symbol"]
            price = usd(lookup(request.form.get("symbol"))["price"])
            return render_template("/quoted", name, symbol, price)

    #Display the quote interface when user requires
    else:
        return render_template("quote.html")
        

Here is my quoted.html

{% extends "layout.html" %}

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

{% block main %}
    <form action="/quoted" method="get">
        <fieldset>
            <div class="form-group">
                <h1> A share of {{ name }} ({{ symbol }}) costs {{ price }}</h1>
            </div>
        </fieldset>
    </form>
{% endblock %}

Here is the return for the lookup function

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

    # Parse response
    try:
        quote = response.json()
        return {
            "name": quote["companyName"],
            "price": float(quote["latestPrice"]),
            "symbol": quote["symbol"]
        }

WillBui
  • 5
  • 1

1 Answers1

1

The render_template() takes one positional argument which is the html file. The rest of the data should be passed as keyword arguments.

Here is the documentation about it: https://flask.palletsprojects.com/en/1.0.x/api/#flask.render_template

So you could do something like:

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

Or make one data structure with your data (dict, list) and work with it in your html

kakou
  • 636
  • 2
  • 7
  • 15
  • Thank you. I understand that the render_template() takes one argument, but in my case I try to pass other arguments into the function. I think the render_template () allows that. The thing is that I cannot figure out what's wrong with my code. – WillBui Jun 24 '21 at 06:59
  • It takes one positional arguments and the rest are keyword arguments. **context in docs. You are passing only positional arguments this is the problem. – kakou Jun 24 '21 at 07:02