2016-12-11 3 views
0

私はpythonとWebプログラミングに新しいです。私は、ユーザーがテキストボックスに入力した内容に基づいてクエリー結果を返し、次にsubmitをヒットする、jinja2とwebapp2ライブラリを使用して、アプリケーションエンジンにいくつかの動作するhtmlとpythonコードを持っています。テキストボックスが更新され、送信ボタンが必要ないときはいつでもPOSTコードが実行されるようにコードを変更したいと思います。これを達成するためのコードサンプルやガイダンスは高く評価されます。テキストフィールドへの更新を提出する - python

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

は、あなたが使用する必要があります

私の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とページと投稿:入力用onkeyupのイベント、あなたのアヤックスへメイクAJAX呼び出し webapp2ハンドラ

+0

ありがとうございました –

関連する問題