librelist archives

« back to archive

how validate forms in Flask-Admin

how validate forms in Flask-Admin

From:
peg kh
Date:
2014-12-18 @ 23:19
I have a simple app with flask Admin I want to check the *validate of form
in create.html* so if it is empty or not valid Show Flash error message But
don't know how I can do that coz didn't use request.method in
flask-admin?Thanx if you could help :)

app.py

 # Models
    class User(db.Model):
       id = db.Column(db.Integer, primary_key=True)
       name = db.Column(db.Unicode(64))
       email = db.Column(db.Unicode(64))
       age = db.Column(db.Integer)
    def __unicode__(self):
       return self.name

    # Customized admin interface
    class CustomView(ModelView):

       list_template = 'list.html'
       create_template = 'create.html'
       edit_template = 'edit.html'

    class UserAdmin(CustomView):

       column_filters = ('name', 'email','age')

    # Flask views

        @app.route('/')
        def index():
           return '<a href="/admin/">Click me to get to Admin!</a>'
        # Create admin with custom base template
        admin = admin.Admin(app, 'Example: Layout', base_template='layout.html')
        # Add views
        admin.add_view(UserAdmin(User, db.session))


        def build_sample_db():
          """Populate a small db with some example entries."""
        db.drop_all()
        db.create_all()
        first_names = [
        'James', 'Amelia', 'Oliver', 'Jack'
        ]

        last_names = [
        'Brown', 'Smith', 'Patel', 'Jones'
        ]
        age = [25, 27, 45, 35]

        for i in range(len(first_names)):
            user = User()
            user.name = first_names[i] + " " + last_names[i]
            user.email = first_names[i].lower() + "@example.com"
            user.age = age[i]
            db.session.add(user)

            db.session.commit()


        if __name__ == '__main__':


           app_dir = op.realpath(os.path.dirname(__file__))
           database_path = op.join(app_dir, app.config['DATABASE_FILE'])

        # Start app
        app.run(debug=True)

 Create.html

{% extends 'admin/model/create.html' %}
{% block brand %}<h2 id="brand">Create {{ admin_view.name|capitalize
}}</h2><div class="clearfix"></div><hr>{% endblock %}
{% block body %}{% call lib.form_tag(form) %}{{
lib.render_form_fields(form, form_opts = form_opts) }}<div
class="form-buttons">{{ lib.render_form_buttons(return_url, extra())
}}
 </div>
 {% endcall %}{ % endblock %}

Re: [flask] how validate forms in Flask-Admin

From:
Paul Brown
Date:
2014-12-18 @ 23:27
You can use form_args to add a "required" validator:

http://flask-admin.readthedocs.org/en/latest/api/mod_model/#flask.ext.admin.model.BaseModelView.form_args

On Thu, Dec 18, 2014 at 5:19 PM, peg kh <peg1988.kh@gmail.com> wrote:
>
> I have a simple app with flask Admin I want to check the *validate of
> form in create.html* so if it is empty or not valid Show Flash error
> message But don't know how I can do that coz didn't use request.method in
> flask-admin?Thanx if you could help :)
>
> app.py
>
>  # Models
>     class User(db.Model):
>        id = db.Column(db.Integer, primary_key=True)
>        name = db.Column(db.Unicode(64))
>        email = db.Column(db.Unicode(64))
>        age = db.Column(db.Integer)
>     def __unicode__(self):
>        return self.name
>
>     # Customized admin interface
>     class CustomView(ModelView):
>
>        list_template = 'list.html'
>        create_template = 'create.html'
>        edit_template = 'edit.html'
>
>     class UserAdmin(CustomView):
>
>        column_filters = ('name', 'email','age')
>
>     # Flask views
>
>         @app.route('/')
>         def index():
>            return '<a href="/admin/">Click me to get to Admin!</a>'
>         # Create admin with custom base template
>         admin = admin.Admin(app, 'Example: Layout', base_template='layout.html')
>         # Add views
>         admin.add_view(UserAdmin(User, db.session))
>
>
>         def build_sample_db():
>           """Populate a small db with some example entries."""
>         db.drop_all()
>         db.create_all()
>         first_names = [
>         'James', 'Amelia', 'Oliver', 'Jack'
>         ]
>
>         last_names = [
>         'Brown', 'Smith', 'Patel', 'Jones'
>         ]
>         age = [25, 27, 45, 35]
>
>         for i in range(len(first_names)):
>             user = User()
>             user.name = first_names[i] + " " + last_names[i]
>             user.email = first_names[i].lower() + "@example.com"
>             user.age = age[i]
>             db.session.add(user)
>
>             db.session.commit()
>
>
>         if __name__ == '__main__':
>
>
>            app_dir = op.realpath(os.path.dirname(__file__))
>            database_path = op.join(app_dir, app.config['DATABASE_FILE'])
>
>         # Start app
>         app.run(debug=True)
>
>  Create.html
>
> {% extends 'admin/model/create.html' %}
> {% block brand %}<h2 id="brand">Create {{ admin_view.name|capitalize 
}}</h2><div class="clearfix"></div><hr>{% endblock %}
> {% block body %}{% call lib.form_tag(form) %}{{ 
lib.render_form_fields(form, form_opts = form_opts) }}<div 
class="form-buttons">{{ lib.render_form_buttons(return_url, extra()) }}
>  </div>
>  {% endcall %}{ % endblock %}
>
>

Re: [flask] how validate forms in Flask-Admin

From:
peg kh
Date:
2014-12-18 @ 23:44
Thanx paul for youy reply, But where should I put it coz I don't have form
in app.py just have it in create.html??? I'm beginner really appreciate if
you could help

{% call lib.form_tag(form) %}{{ lib.render_form_fields(form, form_opts
= form_opts) }}<div class="form-buttons">{{
lib.render_form_buttons(return_url, extra()) }}
 </div>


On Fri, Dec 19, 2014 at 1:27 AM, Paul Brown <paul90brown@gmail.com> wrote:
>
> You can use form_args to add a "required" validator:
> 
http://flask-admin.readthedocs.org/en/latest/api/mod_model/#flask.ext.admin.model.BaseModelView.form_args
>
> On Thu, Dec 18, 2014 at 5:19 PM, peg kh <peg1988.kh@gmail.com> wrote:
>>
>> I have a simple app with flask Admin I want to check the *validate of
>> form in create.html* so if it is empty or not valid Show Flash error
>> message But don't know how I can do that coz didn't use request.method in
>> flask-admin?Thanx if you could help :)
>>
>> app.py
>>
>>  # Models
>>     class User(db.Model):
>>        id = db.Column(db.Integer, primary_key=True)
>>        name = db.Column(db.Unicode(64))
>>        email = db.Column(db.Unicode(64))
>>        age = db.Column(db.Integer)
>>     def __unicode__(self):
>>        return self.name
>>
>>     # Customized admin interface
>>     class CustomView(ModelView):
>>
>>        list_template = 'list.html'
>>        create_template = 'create.html'
>>        edit_template = 'edit.html'
>>
>>     class UserAdmin(CustomView):
>>
>>        column_filters = ('name', 'email','age')
>>
>>     # Flask views
>>
>>         @app.route('/')
>>         def index():
>>            return '<a href="/admin/">Click me to get to Admin!</a>'
>>         # Create admin with custom base template
>>         admin = admin.Admin(app, 'Example: Layout', 
base_template='layout.html')
>>         # Add views
>>         admin.add_view(UserAdmin(User, db.session))
>>
>>
>>         def build_sample_db():
>>           """Populate a small db with some example entries."""
>>         db.drop_all()
>>         db.create_all()
>>         first_names = [
>>         'James', 'Amelia', 'Oliver', 'Jack'
>>         ]
>>
>>         last_names = [
>>         'Brown', 'Smith', 'Patel', 'Jones'
>>         ]
>>         age = [25, 27, 45, 35]
>>
>>         for i in range(len(first_names)):
>>             user = User()
>>             user.name = first_names[i] + " " + last_names[i]
>>             user.email = first_names[i].lower() + "@example.com"
>>             user.age = age[i]
>>             db.session.add(user)
>>
>>             db.session.commit()
>>
>>
>>         if __name__ == '__main__':
>>
>>
>>            app_dir = op.realpath(os.path.dirname(__file__))
>>            database_path = op.join(app_dir, app.config['DATABASE_FILE'])
>>
>>         # Start app
>>         app.run(debug=True)
>>
>>  Create.html
>>
>> {% extends 'admin/model/create.html' %}
>> {% block brand %}<h2 id="brand">Create {{ admin_view.name|capitalize 
}}</h2><div class="clearfix"></div><hr>{% endblock %}
>> {% block body %}{% call lib.form_tag(form) %}{{ 
lib.render_form_fields(form, form_opts = form_opts) }}<div 
class="form-buttons">{{ lib.render_form_buttons(return_url, extra()) }}
>>  </div>
>>  {% endcall %}{ % endblock %}
>>
>>

Re: [flask] how validate forms in Flask-Admin

From:
Margie Roswell
Date:
2014-12-19 @ 00:34
I'm relatively new to flask, so don't have answers, but I'm wondering what
sort of validation you want to do.

I googled

{% call lib.form_tag(form) %}


and found that's part of flask-admin

Then I googled: flask-admin validate and found your stackoverflow request:

http://stackoverflow.com/questions/27557319/how-validate-forms-in-flask-admin

 as well as this one:

http://stackoverflow.com/questions/22947083/flask-admin-modelview-custom-validation/23068912#23068912

I think it would be helpful if you shared exactly what you want to validate
in your form. What field to validate, how, and what the

Might also be possible to use javascript or jquery to validate?

Sorry I don't have anything more specific.


--
http://FarmBillPrimer.org
http://www.BaltimoreUrbanAg.org (Please send events; This site is hungry.)
http://www.ExcellentNutrition.org
http://www.packtpub.com/drupal-5-views-recipes/book

On Thu, Dec 18, 2014 at 6:44 PM, peg kh <peg1988.kh@gmail.com> wrote:
>
> Thanx paul for youy reply, But where should I put it coz I don't have form
> in app.py just have it in create.html??? I'm beginner really appreciate if
> you could help
>
> {% call lib.form_tag(form) %}{{ lib.render_form_fields(form, form_opts =
form_opts) }}<div class="form-buttons">{{ 
lib.render_form_buttons(return_url, extra()) }}
>  </div>
>
>
> On Fri, Dec 19, 2014 at 1:27 AM, Paul Brown <paul90brown@gmail.com> wrote:
>>
>> You can use form_args to add a "required" validator:
>> 
http://flask-admin.readthedocs.org/en/latest/api/mod_model/#flask.ext.admin.model.BaseModelView.form_args
>>
>> On Thu, Dec 18, 2014 at 5:19 PM, peg kh <peg1988.kh@gmail.com> wrote:
>>>
>>> I have a simple app with flask Admin I want to check the *validate of
>>> form in create.html* so if it is empty or not valid Show Flash error
>>> message But don't know how I can do that coz didn't use request.method in
>>> flask-admin?Thanx if you could help :)
>>>
>>> app.py
>>>
>>>  # Models
>>>     class User(db.Model):
>>>        id = db.Column(db.Integer, primary_key=True)
>>>        name = db.Column(db.Unicode(64))
>>>        email = db.Column(db.Unicode(64))
>>>        age = db.Column(db.Integer)
>>>     def __unicode__(self):
>>>        return self.name
>>>
>>>     # Customized admin interface
>>>     class CustomView(ModelView):
>>>
>>>        list_template = 'list.html'
>>>        create_template = 'create.html'
>>>        edit_template = 'edit.html'
>>>
>>>     class UserAdmin(CustomView):
>>>
>>>        column_filters = ('name', 'email','age')
>>>
>>>     # Flask views
>>>
>>>         @app.route('/')
>>>         def index():
>>>            return '<a href="/admin/">Click me to get to Admin!</a>'
>>>         # Create admin with custom base template
>>>         admin = admin.Admin(app, 'Example: Layout', 
base_template='layout.html')
>>>         # Add views
>>>         admin.add_view(UserAdmin(User, db.session))
>>>
>>>
>>>         def build_sample_db():
>>>           """Populate a small db with some example entries."""
>>>         db.drop_all()
>>>         db.create_all()
>>>         first_names = [
>>>         'James', 'Amelia', 'Oliver', 'Jack'
>>>         ]
>>>
>>>         last_names = [
>>>         'Brown', 'Smith', 'Patel', 'Jones'
>>>         ]
>>>         age = [25, 27, 45, 35]
>>>
>>>         for i in range(len(first_names)):
>>>             user = User()
>>>             user.name = first_names[i] + " " + last_names[i]
>>>             user.email = first_names[i].lower() + "@example.com"
>>>             user.age = age[i]
>>>             db.session.add(user)
>>>
>>>             db.session.commit()
>>>
>>>
>>>         if __name__ == '__main__':
>>>
>>>
>>>            app_dir = op.realpath(os.path.dirname(__file__))
>>>            database_path = op.join(app_dir, app.config['DATABASE_FILE'])
>>>
>>>         # Start app
>>>         app.run(debug=True)
>>>
>>>  Create.html
>>>
>>> {% extends 'admin/model/create.html' %}
>>> {% block brand %}<h2 id="brand">Create {{ admin_view.name|capitalize 
}}</h2><div class="clearfix"></div><hr>{% endblock %}
>>> {% block body %}{% call lib.form_tag(form) %}{{ 
lib.render_form_fields(form, form_opts = form_opts) }}<div 
class="form-buttons">{{ lib.render_form_buttons(return_url, extra()) }}
>>>  </div>
>>>  {% endcall %}{ % endblock %}
>>>
>>>

Re: [flask] how validate forms in Flask-Admin

From:
peg kh
Date:
2014-12-19 @ 08:56
Thanx So Much really helpful :))))

On Fri, Dec 19, 2014 at 2:34 AM, Margie Roswell <mroswell@gmail.com> wrote:
>
> I'm relatively new to flask, so don't have answers, but I'm wondering what
> sort of validation you want to do.
>
> I googled
>
> {% call lib.form_tag(form) %}
>
>
> and found that's part of flask-admin
>
> Then I googled: flask-admin validate and found your stackoverflow request:
>
>
> http://stackoverflow.com/questions/27557319/how-validate-forms-in-flask-admin
>
>  as well as this one:
>
> 
http://stackoverflow.com/questions/22947083/flask-admin-modelview-custom-validation/23068912#23068912
>
> I think it would be helpful if you shared exactly what you want to
> validate in your form. What field to validate, how, and what th e
>
> Might also be possible to use javascript or jquery to validate?
>
> Sorry I don't have anything more specific.
>
>
> --
> http://FarmBillPrimer.org
> http://www.BaltimoreUrbanAg.org (Please send events; This site is hungry.)
> http://www.ExcellentNutrition.org
> http://www.packtpub.com/drupal-5-views-recipes/book
>
> On Thu, Dec 18, 2014 at 6:44 PM, peg kh <peg1988.kh@gmail.com> wrote:
>>
>> Thanx paul for youy reply, But where should I put it coz I don't have
>> form in app.py just have it in create.html??? I'm beginner really
>> appreciate if you could help
>>
>> {% call lib.form_tag(form) %}{{ lib.render_form_fields(form, form_opts 
= form_opts) }}<div class="form-buttons">{{ 
lib.render_form_buttons(return_url, extra()) }}
>>  </div>
>>
>>
>> On Fri, Dec 19, 2014 at 1:27 AM, Paul Brown <paul90brown@gmail.com>
>> wrote:
>>>
>>> You can use form_args to add a "required" validator:
>>> 
http://flask-admin.readthedocs.org/en/latest/api/mod_model/#flask.ext.admin.model.BaseModelView.form_args
>>>
>>> On Thu, Dec 18, 2014 at 5:19 PM, peg kh < peg1988.kh@gmail.com> wrote:
>>>>
>>>> I have a simple app with flask Admin I want to check the *validate of
>>>> form in create.html* so if it is empty or not valid Show Flash error
>>>> message But don't know how I can do that coz didn't use request.method in
>>>> flask-admin?Thanx if you could help :)
>>>>
>>>> app.py
>>>>
>>>>  # Models
>>>>     class User(db.Model):
>>>>        id = db.Column(db.Integer, primary_key=True)
>>>>        name = db.Column(db.Unicode(64))
>>>>        email = db.Column(db.Unicode(64))
>>>>        age = db.Column(db.Integer)
>>>>     def __unicode__(self):
>>>>        return self.name
>>>>
>>>>     # Customized admin interface
>>>>     class CustomView(ModelView):
>>>>
>>>>        list_template = 'list.html'
>>>>        create_template = 'create.html'
>>>>        edit_template = 'edit.html'
>>>>
>>>>     class UserAdmin(CustomView):
>>>>
>>>>        column_filters = ('name', 'email','age')
>>>>
>>>>     # Flask views
>>>>
>>>>         @app.route('/')
>>>>         def index():
>>>>            return '<a href="/admin/">Click me to get to Admin!</a>'
>>>>         # Create admin with custom base template
>>>>         admin = admin.Admin(app, 'Example: Layout', 
base_template='layout.html')
>>>>         # Add views
>>>>         admin.add_view(UserAdmin(User, db.session))
>>>>
>>>>
>>>>         def build_sample_db():
>>>>           """Populate a small db with some example entries."""
>>>>         db.drop_all()
>>>>         db.create_all()
>>>>         first_names = [
>>>>         'James', 'Amelia', 'Oliver', 'Jack'
>>>>         ]
>>>>
>>>>         last_names = [
>>>>         'Brown', 'Smith', 'Patel', 'Jones'
>>>>         ]
>>>>         age = [25, 27, 45, 35]
>>>>
>>>>         for i in range(len(first_names)):
>>>>             user = User()
>>>>             user.name = first_names[i] + " " + last_names[i]
>>>>             user.email = first_names[i].lower() + "@example.com"
>>>>             user.age = age[i]
>>>>             db.session.add(user)
>>>>
>>>>             db.session.commit()
>>>>
>>>>
>>>>         if __name__ == '__main__':
>>>>
>>>>
>>>>            app_dir = op.realpath(os.path.dirname(__file__))
>>>>            database_path = op.join(app_dir, app.config['DATABASE_FILE'])
>>>>
>>>>         # Start app
>>>>         app.run(debug=True)
>>>>
>>>>  Create.html
>>>>
>>>> {% extends 'admin/model/create.html' %}
>>>> {% block brand %}<h2 id="brand">Create {{ admin_view.name|capitalize 
}}</h2><div class="clearfix"></div><hr>{% endblock %}
>>>> {% block body %}{% call lib.form_tag(form) %}{{ 
lib.render_form_fields(form, form_opts = form_opts) }}<div 
class="form-buttons">{{ lib.render_form_buttons(return_url, extra()) }}
>>>>  </div>
>>>>  {% endcall %}{ % endblock %}
>>>>
>>>>