首页 > 解决方案 > CS50(2021) - 金融。:( 注册拒绝重复的用户名 - 预期状态码 200,但得到 400

问题描述

我是编程新手,正在做CS50,目前正在努力完成 Pset9 - Finance,2021 年的版本。我已经阅读了尽可能多的关于同一问题的帖子,但没有一个答案能帮助我解决问题.

Application.py 正在做问题简报所要求的事情,我通过了除此之外的所有测试,check50 期待状态码 200,但我发送了 400。

Check50 失败

要记住的事情:

如果有人可以查看下面的代码,我将不胜感激,如果我做错了什么或者只是指出我正确的方向,请告诉我。提前致谢!

这是我在 application.py 中注册的代码:

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

    # Forget any user_id
    session.clear()

    # Registering a user when POST method
    if request.method == "POST":

        # Capturing name and password
        name = request.form.get("username")
        key1 = request.form.get("password")
        key2 = request.form.get("confirmation")

        # If fields are empty render apology
        if not name:
            return apology("Sorry, name is empty", 400)

        elif not key1 or not key2:
            return apology("Sorry, password is empty", 400)

        # If keys are different render an apology
        elif key1 != key2:
            return apology("Sorry, passwords do not match", 400)

        # Once password is valid then hash it before storing it.
        key = generate_password_hash(key1, method='pbkdf2:sha256', salt_length=8)

        # Checking if username exists in the database
        raws = db.execute("SELECT * FROM users WHERE username = :name", name=name)
        if len(raws) == 1:
            return apology("Sorry, username already exists", 400)

        # Include register into our database
        db.execute("INSERT INTO users (username, hash) VALUES(?, ?)", name, key)

        # Query again the user row, so we can pass into a session
        rows = db.execute("SELECT * FROM users WHERE username = :name", name=name)

        # Login the user just registered
        session["user_id"] = rows[0]["id"]

        # return render_template("register.html")
        return redirect("/")

    else:
        return render_template("register.html")

这是 application.py 中的索引:

@app.route("/")
@login_required
def index():

    """Show portfolio of stocks"""
    # Query stock information from user session
    stocks = db.execute("SELECT stock_symbol, stock_name, shares, value, price FROM (SELECT stock_symbol, stock_name, SUM(shares) as shares, SUM(value) as value, price FROM stocks WHERE user_id=:session GROUP by stock_symbol) WHERE shares>0", session=session["user_id"])

    # Loop to sum up all stock value up
    stockValue = 0
    for stock in stocks:
        stockValue += stock["value"]

    # Query cash information from user session and send this to the html
    row_cash = db.execute("SELECT cash FROM users WHERE id=:session", session=session["user_id"])
    cash = row_cash[0]["cash"]

    # Grand total variable adding up stock value and cash
    grand_total = stockValue + cash

    return render_template("index.html", stocks=stocks, cash=usd(cash), grand_total=usd(grand_total))

这是Register.html:

{% 扩展“layout.html”%}

{% block title %}
    Register
{% endblock %}

{% block main %}
    <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="Repeat Password" type="password">
        </div>
        <button class="btn btn-primary" type="submit">Register</button>
    </form>

{% endblock %}

这是 index.html:

{% extends "layout.html" %}
    
    {% block title %}
        Stocks
    {% endblock %}

{% block main %}


            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Symbol</th>
                        <th>Name</th>
                        <th>Shares</th>
                        <th>Price</th>
                        <th>Grand Total</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- Loop through the database entries to display them in this table -->

                    {% for stock in stocks %}
                        <tr>
                            <td>{{ stock.stock_symbol }}</td>
                            <td>{{ stock.stock_name }}</td>
                            <td>{{ stock.shares }}</td>
                            <td>{{ stock.price | usd }}</td>
                            <td>{{ (stock.value) | usd }}</td>
                        </tr>
                    {% endfor %}

                </tbody>
                <tfoot>
                    <tr>
                        <td>Overall stock value</td>
                        <td colspan=3></td>
                        <td>{{ "${:,.2f}".format(stocks|sum('value')) }}</td>
                    </tr>
                    <tr>
                        <td>CASH</td>
                        <td colspan=3></td>
                        <td>{{ cash }}</td>
                    </tr>
                    <tr>
                        <td colspan=4></td>
                        <td>{{ grand_total }}</td>
                    </tr>
                </tfoot>
            </table>


{% endblock %}

标签: pythoncs50

解决方案


当我删除 users.db 中的所有数据然后check50 正确传递时,问题终于得到解决。似乎是数据导致了整个问题,一旦删除,一切都得到了修复。


推荐阅读