简体   繁体   English

与 flask 项目的 href 链接出现错误 404。 似乎找不到页面

[英]Error 404 on href link with flask project. seems not find page

this is my code这是我的代码

sell1.html卖1.html

{% for stock in stocks %}
  <tr>
    <th scope="row" name="stock">{{ stock[0] }}</th>
    <td>{{ stock[1] }}</td>
    <td>{{ stock[2] }}</td>
    <td>{{ stock[3] }}</td>
    <td>{{ stock[4] }}</td>
    <td><a href="sell1/{{ stock[0] }}" class = "btn btn-default pullright">Sell</a> </td>
  </tr>
{% endfor %}

app.py应用程序.py

@app.route("/sell/<string:stock>", methods=["GET", "POST"])
@login_required
def sell1(stock):
    """Sell shares of stock"""

    # User reached route via POST (as by submitting a form via POST)
    if request.method == "POST":
        # collect relevant informations
        amount = int(request.form.get("amount"))
        symbol = request.form.get("symbol")
        price = lookup(symbol)["price"]
        value = round(price * float(amount))

        # Update stocks table
        amount_before = db.execute("SELECT amount FROM stocks WHERE user_id = :user AND symbol = :symbol",
                                   symbol=symbol, user=session["user_id"])[0]['amount']
        amount_after = amount_before - amount

        # delete stock from table if we sold every unit we had
        if amount_after == 0:
            db.execute("DELETE FROM stocks WHERE user_id = :user AND symbol = :symbol",
                       symbol=symbol, user=session["user_id"])

        # stop the transaction if the user does not have enough stocks
        elif amount_after < 0:
            return apology("That's more than the stocks you own")

        # otherwise update with new value
        else:
            db.execute("UPDATE stocks SET amount = :amount WHERE user_id = :user AND symbol = :symbol",
                       symbol=symbol, user=session["user_id"], amount=amount_after)

        # calculate and update user's cash
        cash = db.execute("SELECT cash FROM users WHERE id = :user",
                          user=session["user_id"])[0]['cash']
        cash_after = cash + price * float(amount)

        db.execute("UPDATE users SET cash = :cash WHERE id = :user",
                   cash=cash_after, user=session["user_id"])

        # Update history table
        db.execute("INSERT INTO transactions(user_id, symbol, amount, value) VALUES (:user, :symbol, :amount, :value)",
                   user=session["user_id"], symbol=symbol, amount=-amount, value=value)

        # Redirect user to home page with success message
        flash("Sold!")
        return redirect("/")

    # User reached route via GET (as by clicking a link or via redirect)
    else:

        # Add Symbol

        # query database with the transactions history
        rows = db.execute("SELECT symbol, amount FROM stocks WHERE (user_id = :user AND symbol = :stock)",
                          user=session["user_id"], stock=stock)
        stocks = {}
        for row in rows:
            stocks[row['symbol']] = row['amount']

        return render_template("sell1.html", stocks=stocks)

and

in index.html:在索引中。html:

{% for stock in stocks %}
  <tr>
    <th scope="row" name="stock">{{ stock[0] }}</th>
    <td>{{ stock[1] }}</td>
    <td>{{ stock[2] }}</td>
    <td>{{ stock[3] }}</td>
    <td>{{ stock[4] }}</td>
    <td><a href="sell1/{{ stock[0] }}" class = "btn btn-default pullright">Sell</a> </td>
  </tr>

index page works fine as it send this Get:索引页面在发送此 Get 时工作正常:

"GET /sell1/AAPL HTTP/1.1" 404 - “GET /sell1/AAPL HTTP/1.1”404 -

But after I have issue as it is a 404 page with this url: http://127.0.0.1:5000/sell1/AAPL但是在我遇到问题后,因为它是一个 404 页面,这个 url: http://127.0.0.1:5000/sell1/AAPL

I tried to check sql query and it works fine.我试图检查 sql 查询,它工作正常。 this is schema of stocks table: 'stocks' ('id' integer PRIMARY KEY AUTOINCREMENT NOT NULL, 'user_id' integer NOT NULL, 'symbol' char(4) NOT NULL, 'amount' integer NOT NULL); this is schema of stocks table: 'stocks' ('id' integer PRIMARY KEY AUTOINCREMENT NOT NULL, 'user_id' integer NOT NULL, 'symbol' char(4) NOT NULL, 'amount' integer NOT NULL);

thanks for help as I m new in flask and python...感谢您的帮助,因为我是 flask 和 python 的新手...

thanks to mouse tail, In my route, I forgot sell1感谢鼠标尾巴,在我的路线上,我忘记了卖1

@app.route("/sell1/<string:stock>", methods=["GET", "POST"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM