def index():
form = SearchForm()
if form.validate_on_submit():
old_word = session.get('key_word')
old_price = session.get('price')
if old_word == form.key_word.data and \
old_price == form.price.data:
flash('It seems like you searched the same keyword and \
price for the two time!')
session['price'] = form.price.data
session['key_word'] = form.key_word.data
return redirect(url_for('search',
key_word=form.key_word.data,
price=form.price.data, refresh=form.refresh.data))
return render_template('index.html', form=form)
python类SearchForm()的实例源码
def onStart(self):
"""
Initialize the forms.
"""
self.addForm('MAIN', SearchForm, name="Search for ROM")
def search():
form = SearchForm(request.form)
reports = []
if form.validate_on_submit():
results = search_provider.search(form['query'].data)
for result in results:
report = {f.name: f.value for f in result.fields}
report['id'] = result.doc_id
reports.append(report)
return render_template('search.html', reports=reports)
return render_template('search.html', reports=reports)
def buscador():
form = SearchForm()
results = None
if form.validate_on_submit():
name = form.name.data
price_max = form.price_max.data or ''
# Search in Wallapop
results = get_resultados(name, price_max)
return render_template('items/buscador.html', form=form, results=results)
def base_options():
# a function for the options required by every page
return {
'pages': Page.query.all(),
'current_user': current_user,
'search_form': forms.SearchForm()
}
# TODO: try to use: app.jinja_env.globals['func_name'] = func
# ---------------------------------------------- ROUTING FUNCTIONS
def search():
# ajax request handling
if g.sijax.is_sijax_request:
g.sijax.register_callback('update_notifications', update_notifications)
g.sijax.register_callback('set_all_notifications_seen', set_all_notifications_seen)
return g.sijax.process_request()
# non-ajax handling:
# posts = Post.query.search(interrogation).all()
form = forms.SearchForm()
options = {
'title': 'Search',
'search_form': form,
}
if form.validate_on_submit():
selection = form.filter.data
interrogation = form.interrogation.data
if not selection or selection == 'all':
options.update({
'users': User.query.search(interrogation, sort=True)[:5],
'topics': Topic.query.search(interrogation, sort=True)[:5],
'posts': Post.query.search(interrogation, sort=True)[:5],
's_pages': Page.query.search(interrogation, sort=True)[:5],
'proposals': Proposal.query.search(interrogation, sort=True)[:5],
'laws': Law.query.search(interrogation, sort=True)[:5]
})
elif selection == 'users':
options['users'] = User.query.search(interrogation, sort=True)[:20]
elif selection == 'topics':
options['topics'] = Topic.query.search(interrogation, sort=True)[:20]
elif selection == 'posts':
options['posts'] = Post.query.search(interrogation, sort=True)[:20]
elif selection == 'pages':
options['s_pages'] = Page.query.search(interrogation, sort=True)[:20]
elif selection == 'proposals':
options['proposals'] = Proposal.query.search(interrogation, sort=True)[:20]
elif selection == 'laws':
options['laws'] = Law.query.search(interrogation, sort=True)[:20]
options.update(base_options())
return render_template("search.html", **options)
def search():
# ????????????????????
form = SearchForm()
if form.validate_on_submit():
old_word = session.get('key_word')
old_price = session.get('price')
if old_word == form.key_word.data and \
old_price == form.price.data:
flash('It seems like you searched the same keyword and \
price for the two time!')
session['price'] = form.price.data
session['key_word'] = form.key_word.data
return redirect(url_for('search',
key_word=form.key_word.data,
price=form.price.data,
refresh=form.refresh.data))
key_word = request.args.get('key_word', None)
price = float(request.args.get('price', '0'))
refresh = request.args.get('refresh', 'False')
page = int(request.args.get('page', 1))
if refresh == 'True':
result, end_page = Search(price, key_word, page), 5
else:
data = db.search_goods(price, key_word)
end_page = len(data)/8 if len(data) % 8 == 0 else len(data)/8 + 1
# ????????????8????
try:
result = data[8*(page-1):8*page]
except:
result = data[8*(page-1):]
end_page_list = range(1, end_page+1)
prev_page = page - 1 if page - 1 else 1
next_page = page + 1 if page + 1 <= end_page else end_page
return render_template(
'search.html',
form=form,
key_word=key_word,
price=price,
refresh=refresh,
page=page,
result=result,
end_page_list=end_page_list,
prev_page=prev_page,
next_page=next_page)