0

I have been stuck on this for a while now. No matter how I change my code nothing works it's the same 3 errors. I think the issue is in my register.html but I can't find anything wrong with it. Here are the errors I get in check50:

:( registering user succeeds application raised an exception (rerun with --log for more details) Log sending POST request to /register exception raised in application: TemplateSyntaxError: expected token ',', got 'string'

:( /check route confirms whether username is available application raised an exception (rerun with --log for more details) Log sending GET request to /check?username=check50student1 checking that status code 200 is returned... sending POST request to /register exception raised in application: TemplateSyntaxError: expected token ',', got 'string'

:( registration rejects duplicate username application raised an exception (rerun with --log for more details) Log sending POST request to /register exception raised in application: TemplateSyntaxError: expected token ',', got 'string'

Here is my register code: ``@app.route("/register", methods=["GET", "POST"]) def register(): """Register user"""

if request.method == "POST":

    # Enter username
    if not request.form.get("username"):
        return apology("must provide username")

    # Enter password
    elif not request.form.get("password"):
        return apology("must provide password")

    # Confirm password
    elif not request.form.get("password") == request.form.get("confirmation"):
        return apology("passwords do not match")

    # Hash password and enter into database
    hash = generate_password_hash(request.form.get("password"))
    new_user = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)",
                          username=request.form.get("username"), hash=hash)

    # Username unique
    if not new_user:
        return apology("username taken")

    session["user_id"] = new_user

    flash("Registered!")

    return redirect(url_for("index"))

else:
    return render_template("register.html")``

Here is my register.html code:

> {% extends "layout.html" %}
> 
> {% block title %}
>     Register {% endblock %}
> 
> {% block main %}
>     <script>
>     $(document).ready(function(){
>         $('#myForm').submit(function(event) {
>             event.preventDefault();
>             let username = $("#username").val();
>             $.get('/check?username=' + username, function(data) {
>                 if(data == false) {
>                     alert("Username taken.");
>                 }
>                 else {
>                     console.log("Username free.");
>                     $('#myForm').submit();
>                 }
>             });
>         });
>     });
>     </script>
>     <form action="/register", method="post">
>         <div class="form-group">
>             <input autocomplete="off" autofocus class="form-control" name="username" placeholder="Username" type="text">
>         </div>
>         <div class="form-group">
>             <input class="form-control" name="password" placeholder="Password" type="password">
>         </div>
>         <div class="form-group">
>             <input class="form-control" name="confirmation" placeholder="Password (again)" type="password">
>         </div>
>         <button class="btn btn-primary" type="submit">Register</button>
>     </form> {% endblock %}

Here is my code for index:

@app.route("/")
@login_required
def index():
    """Show portfolio of stocks"""

    # Lookup users
    users = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])
    stocks = db.execute(
        "SELECT symbol, SUM(shares) as total_shares FROM transactions WHERE user_id = :user_id GROUP BY symbol HAVING total_shares > 0", user_id=session["user_id"])
    quotes = {}

    for stock in stocks:
        quotes[stock["symbol"]] = lookup(stock["symbol"])

    cash_left = users[0]["cash"]
    total = cash_left

    return render_template("portfolio.html", quotes=quotes, stocks=stocks, total=total, cash_left=cash_left)

And here is my portfolio.html:

{% extends "layout.html" %}

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

{% block main %}
    <table class="table table-striped">
        <thead>
            <tr>
                <td>Symbol</td>
                <td>Shares</td>
                <td>Price</td>
                <td>Actions</td>
                <td>Total</td>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td colspan="4"></td>
                <td>{{ cash_left | usd }}</td>
            </tr>
        </tfoot>
        <tbody>
            {% for stock in stocks %}
            <tr>
                <td>{{ stock.symbol }}</td>
                <td>{{ stock.total_shares }}</td>
                <td>{{ quotes[stock.symbol]["price] | usd }}</td>
                <td>
                    <form action="/buy" method="post">
                        <input type="hidden" name="symbol" value="{{ stock.symbol }}">
                        <input type="number" name="shares" min="1" placeholder="0" required/>
                        <button class="btn btn-sm btn-primary" type="submit">Buy</button>
                    </form>
                    <form action="/sell" method="post">
                        <input type="hidden" name="symbol" value="{{ stock.symbol }}">
                        <input type="number" name="shares" min="1" placeholder="0" required/>
                        <button class="btn btn-sm btn-primary" type="submit">Sell</button>
                    </form>
                </td>
                <td>{{ (quotes[stock.symbol]["price] * stock.total_shares) | usd }}</td>
            </tr>
            {% endfor %}

            <tr>
                <td colspan="4">Cash</td>
                <td>{{ total| usd }}</td>
            </tr>
        </tbody>
    </table>
{% endblock %}```

4
  • Is it possible that there is a problem in index, or its template, for some reason?
    – Blauelf
    Commented Oct 2, 2019 at 6:09
  • I'm not sure. It looks correct to me. I'll add my code for index and the html for portfolio to my question above.
    – Cori
    Commented Oct 2, 2019 at 18:02
  • I can think of something that may be the issue but I don't know how to fix it since this is the portion of the code that was input by CS50 that I don't think I should change, but maybe I'm missing something else. Next to this line of code app.jinja_env.filters["usd"] = usd I have a red error bubble that says method jinja_env has no filters member. What does that mean? Do I need to add something special somewhere?
    – Cori
    Commented Oct 2, 2019 at 19:06
  • That part likely is fine. The editor simply does not know about the code defining that object.
    – Blauelf
    Commented Oct 2, 2019 at 22:49

1 Answer 1

0

Since it is a TemplateSyntaxError, and the template to be rendered is portfolio.html, the error should appear there.

I assume the mistake responsible is writing ["price] instead of ["price"] (or using Jinja's "magic" also .price).

1
  • This was it! I can't believe I missed it. I had to change it to stock.price and all green now. Thank you so much for all of your help!
    – Cori
    Commented Oct 2, 2019 at 23:38

You must log in to answer this question.

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