2016-12-11 61 views
0

我是python和web編程的新手。我在app引擎中有一些可用的html和python代碼,使用jinja2和webapp2庫,根據用戶輸入到文本框中的內容返回查詢結果,然後點擊提交。我想更改我的代碼,以便在更新文本框時不需要提交按鈕即可運行POST代碼。任何代碼示例或如何完成此指導將不勝感激。對文本字段進行任何更新提交 - python

我的HTML體

<body> 
<div class="container"> 
    <form action="/" method="post"> 
    <div><input type="text" name="content" class="input-block-level" rows="1" value="{{ textInput }}"></textarea></div> 
    <div><input type="submit" class="btn btn-large btn-primary" value="Submit"></div> 
    <div><label>Query Time(ms): </label><label>{{ queryTime }}</label></label></div> 

    </form> 
    {% for product in products %} 
    <div class="row"> 
    <blockquote>{{ product }}</blockquote> 
    </div> 
    {% endfor %} 

我的Python使用get主頁,發佈

class MainPage(webapp2.RequestHandler): 

def get(self): 
    template = JINJA_ENVIRONMENT.get_template('index.html') 
    textInput = "" 
    products = "" 
    queryTime = 0 
    template_values = { 
     'products': products, 
     'textInput': textInput, 
     'queryTime': queryTime, 
    } 

    self.response.write(template.render(template_values)) 

def post(self): 
    textInput = self.request.get('content').lower() 
    template = JINJA_ENVIRONMENT.get_template('index.html') 

    t1= time.time() 
    product_query = ndb.gql("SELECT * FROM Product where name >= :1 LIMIT 5",textInput) 
    results = product_query.fetch(5) 
    t2= time.time() 

    products = [] 

    for counter, names in enumerate(results): 
     if counter == 0: 
      if not names.name.lower().startswith(textInput.lower()): 
       products.append("No Matches") 
     if names.name.startswith(textInput.lower()): 
      products.append(names.name) 


    queryTime = (t2 - t1) * 1000 

    template_values = { 
     'products': products, 
     'textInput': textInput, 
     'queryTime': queryTime, 
    } 

    self.response.write(template.render(template_values)) 

app = webapp2.WSGIApplication([ 
    ('/', MainPage), 
], debug=True) 

回答

1

您應該使用:輸入onkeyup事件,這使得Ajax調用阿賈克斯webapp2處理程序

+0

謝謝,生病讀過那些 –