当前位置: 首页>>代码示例>>Python>>正文


Python Form.validate方法代码示例

本文整理汇总了Python中forms.Form.validate方法的典型用法代码示例。如果您正苦于以下问题:Python Form.validate方法的具体用法?Python Form.validate怎么用?Python Form.validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在forms.Form的用法示例。


在下文中一共展示了Form.validate方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: categories_edit

# 需要导入模块: from forms import Form [as 别名]
# 或者: from forms.Form import validate [as 别名]
def categories_edit(category_id):
    try:
        category = Category.find_by_id(category_id)
        if category:
            if request.method == 'GET':
                category_dict = category.__dict__
                form = Form(category_dict, form_fields)
                return render_template('category/edit.html', form=form, category_id=category_id, success=True)
            elif request.method == 'POST':
                form = Form(request.form, form_fields)
                if form.validate():
                    values = form.named_values()
                    category.from_dict(values)
                    category.save()
                    miner = miner_cls(category)
                    miners[category_id] = miner
                    miner.start()
                    return redirect(url_for('categories'))
                else:
                    return render_template('category/edit.html', form=form, category_id=category_id, success=True)
            elif request.method == 'DELETE':
                miner = miners[category_id]
                miner.stop()
                del miners[category_id]
                Category.delete(category_id)
                return 'Category {} deleted.'.format(category_id), 200
            else:
                return 'Unsupported request method.', 400
        else:
            return 'Category {} not found.'.format(category_id), 400
    except ValueError, e:
        print e
        return '{} is not a valid category id.'.format(category_id), 400
开发者ID:tirami,项目名称:udadisi-google-scholar,代码行数:35,代码来源:views.py

示例2: categories_edit

# 需要导入模块: from forms import Form [as 别名]
# 或者: from forms.Form import validate [as 别名]
def categories_edit(category_id):
    try:
        category = Category.find_by_id(category_id)
        if category:
            if request.method == "GET":
                category_dict = category.__dict__
                form = Form(category_dict, form_fields)
                return render_template("category/edit.html", form=form, category_id=category_id, success=True)
            elif request.method == "POST":
                form = Form(request.form, form_fields)
                if form.validate():
                    values = form.named_values()
                    category.from_dict(values)
                    category.save()
                    miner = miner_cls(category)
                    miners[category_id] = miner
                    miner.start()
                    return redirect(url_for("categories"))
                else:
                    return render_template("category/edit.html", form=form, category_id=category_id, success=True)
            elif request.method == "DELETE":
                if category_id in miners:
                    miner = miners[category_id]
                    miner.stop()
                    del miners[category_id]
                    Category.delete(category_id)
                    return "Category {} deleted.".format(category_id), 200
                else:
                    return "Category {} not present on miner.".format(category_id), 400
            else:
                return "Unsupported request method.", 400
        else:
            return "Category {} not found.".format(category_id), 400
    except ValueError, e:
        print e
        return "{} is not a valid category id.".format(category_id), 400
开发者ID:tirami,项目名称:udadisi-rss,代码行数:38,代码来源:views.py

示例3: BaseCrud

# 需要导入模块: from forms import Form [as 别名]
# 或者: from forms.Form import validate [as 别名]
class BaseCrud(object):
    
    def __init__(self, name, **kwargs):
        super(BaseCrud, self).__init__()
        self.name=name
        self.pk_type='int'
        self.navigation_hint=name+'.list'
        self.form=[]
        self.label=name.capitalize()
        self.url='/{0}/'.format(name)
        self.parent=None
        self.labels={}
        self.list_columns=[]
        self.form=Form()
        self.collection=None


        if 'pk_type' in kwargs: self.pk_type=kwargs['pk_type'] 
        if 'navigation_hint' in kwargs: self.navigation_hint=kwargs['navigation_hint'] 
        if 'label' in kwargs: self.label=kwargs['label'] 
        if 'url' in kwargs: self.url=kwargs['url'] 
        if 'labels' in kwargs: self.labels=kwargs['labels'] 
        if 'list_columns' in kwargs:
            self.list_columns=create_columns(kwargs['list_columns'])
        if 'form' in kwargs: 
            self.form=create_form(kwargs['form'],self.labels)  
        



    def list(self):
        context = self.__get_context()
        context['rows']=self.collection.list()
        return self.render('list', context) 

    def view(self, id):
        obj=self.collection.find(self.pk_prop,id)
        context = self.__get_context()
        context['obj']=obj
        return 'view '+str(id)+' '+str(context)

    def edit(self, id):
        obj=self.collection.find(self.pk_prop,id)
        context = self.__get_context()
        context['obj']=obj
        context['form']=self.form
        context['form_data']=self.form.form_data(obj)
        
        if request.method == 'POST':
            data=self.form.form_data(request.form)
            errors=self.form.validate(data)
            if not errors:
                self.form.set_obj(request.form, obj)
                self.collection.update(obj)
                flash('<strong>{name}</strong> updated!'.format(name=self.get_label(obj)))
                return redirect(url_for('%s.list' % self.name, id=id))
            else:
                context['form_data']=data
                context['form_errors']=errors

        return self.render('edit', context) 

    def delete(self, id):
        obj=self.collection.find(self.pk_prop,id)
        context = self.__get_context()
        context['obj']=obj
        self.collection.delete(obj)
        flash('<strong>{name}</strong> deleted!'.format(name=self.get_label(obj)))
        return redirect(url_for('%s.list' % self.name))

    def new(self):
        obj=self.collection.new_object()
        context = self.__get_context()
        context['obj']=obj
        context['form']=self.form
        context['form_data']=self.form.form_data(obj)

        if request.method == 'POST':
            data=self.form.form_data(request.form)
            errors=self.form.validate(data)
            if not errors:
                self.form.set_obj(request.form, obj)
                self.collection.save(obj)
                flash('<strong>{name}</strong> created!'.format(name=self.get_label(obj)))
                return redirect(url_for('%s.list' % self.name))
            else:
                context['form_data']=data
                context['form_errors']=errors
                            
    
        return self.render('new', context) 

    def __get_context(self):
        ctx= { 'crud_name':self.name, 'crud':self }
        if self.parent:
            ctx['app']=self.parent
        if self.navigation_hint:
            ctx['navigation_hint']=self.navigation_hint
        return ctx    

#.........这里部分代码省略.........
开发者ID:artfh,项目名称:flask-crud,代码行数:103,代码来源:crud.py


注:本文中的forms.Form.validate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。