python中sqlite数据库中select实现输入选取_从下拉建议中选择项目后,在python中更新sqlite数据库...

My program must update table "idlist" in python after the item was selected from drop down suggestions in js. User selects the item, after that POST request in python adds it to the "idlist" table.

As I run the program I get the following error message:

I am grateful for your ideas and suggestions:

Here is my python code:

def search():

"""Search for books that match query"""

if request.method == "GET":

if not request.args.get("q"):

return render_template("adjust.html")

else:

q = request.args.get("q") + "%"

books = db.execute("SELECT Title, Author FROM book WHERE Title LIKE :q OR Author LIKE :q", q=q)

return jsonify(books)

if request.method == "POST" and request.form.get("title"):

Title = request.form.get("title")

insert_book = db.execute("INSERT INTO idlist (id,Title1, Status) VALUES (:id, :Title1, :Status)", id=session["user_id"], Title1=Title, Status="Not started")

return redirect("/")

return render_template("adjust.html")

Here is html:

{% extends "layout.html" %}

{% block title %}

Add your Book

{% endblock %}

{% block main %}

Choose your Book

Title, Author

Add

{% endblock %}

Here is js:

function configure()

{

// Configure typeahead

$("#q").typeahead({

highlight: false,

minLength: 1

},

{

display: function(suggestion) { return suggestion.Title; },

limit: 10,

source: search,

templates: {

suggestion: Handlebars.compile(

"

"+

"{{Title}}, {{Author}}" +

"

"

)

}

});

// Give focus to text box

$("#q").focus();

}

// Search database for typeahead's suggestions

function search(query, syncResults, asyncResults)

{

// Get places matching query (asynchronously)

let parameters = {

q: query

};

$.getJSON("/adjust", parameters, function(data, textStatus, jqXHR) {

// Call typeahead's callback with search results (Author Title)

asyncResults(data);

});

}

$(document).ready(function() {

configure();

});

解决方案

Don't use Ajax to Add new book it will make user confused because he don't know if it was added to the idlist or not, use the Form POST instead.

in script.js remove the following block

$("#q").on('typeahead:selected', function a(eventObject, suggestion, name) {

...

...

});

and to add selected item to the input form, replace

display: function(suggestion) { return null; },

with

display: function(suggestion) { return suggestion.Title; },

to make form POST, in adjust.html replace

....

with

....

And the addBook() method

@app.route("/addbook", methods=["GET", "POST"])

def addbook():

"""Add selected book"""

if request.method == "POST" and request.form.get("title"):

Title = request.form.get("title")

insert_book = db.execute("INSERT INTO idlist (id,Title1, Status) VALUES (:id, :Title1, :Status)", id=session["user_id"], Title1=Title, Status="Not started")

return redirect("/")

# no "title" in the form, return to Add new book page

return redirect("/adjust")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值