Flask AppBuilder Base views

Source: Internet
Author: User

Base views

All views inherit from this class. Its constructor will register your URL as a blueprint in flask, as well as all security permissions that need to be defined and protected.

You can use this view to implement your own custom page, attach it to a menu, or link it to your site.

Use the @expose to decorate your URL routing method. Also, add a @hasaccess decorator to tell the flask that this is a safe way to secure it.

Use Flask-appbuilder-skeleton. Edit the view. PY file and add:

 fromFlask_appbuilderImportAppBuilder, expose, Baseview fromAppImportAppbuilderclassMyView (Baseview): Route_base="/myview"@expose ('/method1/<string:param1>')    defmethod1 (Self, param1):#Do something with param1        #and return it        returnparam1 @expose ('/method2/<string:param1>')    defmethod2 (Self, param1):#Do something with param1        #and render itparam1 ='Hello%s'%(param1)returnParam1appbuilder.add_view_no_menu (MyView ())

This simple example registers your view with two route URLs:

/myview/method1/<string:param1>/myview/method2/<string:param1>

No menus are created for this purpose, and security permissions are not created. If you want to enable detailed security access for your method, use the @hasaccess decorator. Now run this example

$ fabmanager Run

You can use the following URL to test your method

Http://localhost:8080/myview/method1/john

Http://localhost:8080/myview/method2/john

As you can see, these methods are public. So let's protect them. Change the views.py,

 fromFlask_appbuilderImportAppBuilder, Baseview, expose, has_access fromAppImportAppbuilderclassMyView (Baseview): Default_view='method1'@expose ('/method1/') @has_accessdefmethod1 (self):#Do something with param1        #and return to previous page or index        return 'Hello'@expose ('/method2/<string:param1>') @has_accessdefmethod2 (Self, param1):#Do something with param1        #and render template with Paramparam1 ='Goodbye%s'%(param1)returnParam1appbuilder.add_view (MyView,"Method1", category='My View') Appbuilder.add_link ("Method2", href='/myview/method2/john', category='My View')

Note that these methods will make it impossible to integrate the look and feel of a simple page with a fab. It's easy to integrate the look and feel of your application into your application's responses, so you'll have to create your own templates. Create a folder named "Templates" under your project directory and application folder. inside Create a filename ' method3.html '

{% extends  "appbuilder/base.html"  %} {% block  content  %}     <   ></>  {% endblock  %}

in the MyView class , add the following methods :

From flask import render_template@expose ('/method3/<string:param1>' ) @has_accessdef method3 (self, param1):    # does something with param1    # and render template with param    param1 = ' G Oodbye%s '% (param1)    self.update_redirect ()    return self.render_template (' method3.html ', param1 = param1) 

Create a menu link to your new method:

Appbuilder.add_link ("Method3", href= '/myview/method3/john ', category= ' My View ')

You can see that you only need to extend "appbuilder/base.html" on your template and then overwrite Block Content . You have many other blocks that can overwrite or extend things like CSS include,javascript,headers,tail and so on ... Next, use Flaskrender_template to render your new template.

Note Update redirection, in version 0.10.3, the redirection algorithm is reviewed, and a session cookie is used to save 5 navigation history. This is useful for redirecting back, maintaining URL parameters, and improving the UI experience. You must call Self.update_redirect () to insert the current URL into the navigation history. Sometimes you may want to skip updates, such as form validation errors, so that background operations do not send you to the same form until validation errors occur.
Note Since the 1.3.0 version, you must render all the view templates, such as Self.render_template, because Base_template (which can be overwritten) and Appbuilder are now always passed to the template.

Subclasses Simpleformview or Publicformview provide the underlying processing for your custom Form view.

Typically, you will need this view to render forms that are not based on the database model, because FAB can automatically generate them when they are executed, and you can add or remove fields and custom validators to them. to do this, you can use Modelview.

 fromWtformsImportForm, Stringfield fromWtforms.validatorsImportdatarequired fromFlask_appbuilder.fieldwidgetsImportBs3textfieldwidget fromFlask_appbuilder.formsImportDynamicformclassMyForm (dynamicform): field1= Stringfield (('Field1'), Description=('Your Field number one!'), validators= [DataRequired ()], widget=bs3textfieldwidget ()) Field2= Stringfield (('Field2'), Description=('Your Field number two!'), Widget=bs3textfieldwidget ())

Now define your form view to expose URLs, create a menu entry, create secure access, define pre and post processing.

Implement Formget and Formpost to implement form preprocessing and post-processing. You can use Formget to pre-populate the form, pre-process some content in the application, and then use Formpost to submit the form to the form after the user submits it, and you can save the data to the database, send e-mail, or other actions that you need.

 fromFlask_appbuilderImportSimpleformview fromFlask_babelImportLazy_gettext As _classMyFormView (simpleformview): Form=MyForm Form_title='This is my first Form view'message='My Form submitted'    defForm_get (self, form): Form.field1.data='This is prefilled'    defform_post (self, form):#Post Process FormFlash (Self.message,'Info') Appbuilder.add_view (MyFormView,"My Form View", icon="Fa-group", Label=_ ('My Form View'), category="My Forms", category_icon="Fa-cogs")

Please note that this class derives from Baseview, Therefore, you can override all the properties of the parent class. Also note how the label uses Babel's Lazy_gettext as the _ (' text ') feature so that your menu items can be translated.

you can be in Find This example on simpleform .

Flask AppBuilder Base views

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.