本文整理匯總了Python中forms.ProductForm.validate方法的典型用法代碼示例。如果您正苦於以下問題:Python ProductForm.validate方法的具體用法?Python ProductForm.validate怎麽用?Python ProductForm.validate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類forms.ProductForm
的用法示例。
在下文中一共展示了ProductForm.validate方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: share
# 需要導入模塊: from forms import ProductForm [as 別名]
# 或者: from forms.ProductForm import validate [as 別名]
def share():
if request.method == 'GET':
form = ProductForm()
else:
form = ProductForm(request.form)
if form.validate():
if form.create_product(g.user) is not None:
return redirect(url_for('home'))
return render_template('share_product.html', form=form)
示例2: ProductHandler
# 需要導入模塊: from forms import ProductForm [as 別名]
# 或者: from forms.ProductForm import validate [as 別名]
class ProductHandler(BaseHandler):
@admin_required
def get(self, product_id=None, **kwargs):
"""Return a product to edit or an empty form to create"""
template = 'admin/product/new.html'
context = {
'form': self.form,
}
# render edit form
if product_id is not None:
product = Product.get_by_id(product_id)
if product:
self.form = ProductForm(obj=product)
self.form.tags.data = ', '.join(product.tags)
context.update({ 'form': self.form })
template = 'admin/product/edit.html'
else:
return redirect('/admin/shop/')
# render new form
return self.render_response(template, **context)
@admin_required
def post(self, product_id=None, **kwargs):
"""Handle submitted form data"""
# validate form
if self.form.validate():
name = self.form.name.data
description = self.form.description.data
price = self.form.price.data
unit = self.form.unit.data
live = self.form.live.data
tags = self.form.tags.data
language = self.form.language.data
if tags is not None:
tags = [tag.strip() for tag in tags.split(',') if tag != '']
# save edit form
if product_id:
product = Product.get_by_id(product_id)
product.name = name
product.description = description
product.price = price
product.unit = unit
product.live = live
product.tags = tags
product.language = language
# save new form
else:
product = Product(name=name, description=description, price=price, unit=unit, live=live, tags=tags, language=language)
if product.put():
return redirect('/admin/shop/products/')
return self.get(**kwargs)
@cached_property
def form(self):
"""Form instance as cached_property"""
return ProductForm(self.request)