簡體   English   中英

使用來自URL的變量進行查詢無法執行-Flask-SQLAlchemy

[英]Query using variable from url not executing - Flask-SQLAlchemy

我有jquery向api端點發送請求。 代碼如下:

$.get("http://localhost:5000/api/update/"+ elm2, function(data){});

端點的定義如下:

@app.route('/api/update/<id>', methods=['GET'])
def check_new_entries(id):
    result = Trades.query.filter_by(id=id).first_or_404()
    new_entries = Trades.query.filter(Trades.time_recorded > result.time_recorded).all()

要查詢的表是:

class Trades(db.Model):
    id = db.Column(db.String,default=lambda: str(uuid4().hex), primary_key=True)
    amount = db.Column(db.Integer, unique=False)
    time_recorded = db.Column(db.DateTime, unique=False)

問題:Jquery成功發送了正確的請求,變量的類型為string,但是執行第一個查詢時,它無法返回任何內容。 我在另一個應用程序中具有類似的終結點,並且工作正常。.為什么這是例外? 可能缺少什么? 我確定我要查詢的記錄在數據庫中,因此它應該返回它。

@app.route('/api/update/<id>', methods=['GET',])
def check_new_entries(id):
    result = Trades.query.filter_by(id=id).first()
    if result:
        new_entries = Trades.query.filter(Trades.time_recorded >= result.time_recorded).all()

問題在於字符串的其余部分之前有一些意外的空間。 該錯誤源於jquery和jinja,其中jquery正在獲取元素的html內容並將其作為變量發送。 jinja語法類似於: <p> {{ variable }}</p>當jquery提取html內容時,它會在btwn段落開始標簽和花括號之間加上空格。 傳輸的變量的前導空格導致了錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM