Git Product home page Git Product logo

pform's Introduction

pform

https://travis-ci.org/fafhrd91/pform.png

Simple form example

Form contains three different subsystems, basic form attributes, fields and actions.

import pform
from pyramid.httpexceptions import HTTPFound


class EditForm(pform.Form):

     label = 'Edit form'

     fields = pform.Fieldset(
         pform.TextField(
            'name', title='Name')
     )

     @pform.button('Save')
     def safe_handler(self):
         data, errors = self.extract()

         if errors:
             self.add_error_message(errors)

         self.context.name = data['name']

     @pform.button('Cancel')
     def cancel_handler(self):
         return HTTPFound(location='.')

This form renders one field name and two actions safe and cancel (submit buttons). Now we can render this form:

from pyramid.view import view_config

@view_config(route_name='....', renderer='myview.jinja2')
def my_view(request):
    form = EditForm(some_context, request)
    return form()

also it is possible to use form class as view:

@view_config(route_name='....')
class EditForm(pform.Form):

    fields = ...

To do additional custom form initialization just override update method, also if update method returns dictionary this values bypasses to form template:

class EditForm(pform.Form):

    def update(self):
        # custom form initialization
        return {some_data: some_value}

To get form values use extract method, this method return data and errors. data is a form result, errors is a list of errors:

class EditForm(pform.Form):

    @pform.button('Save')
    def save_handler(self):
        data, errors = self.extract()

        if errors:
            self.add_error_message(errors)

        # save data
        ...

Customization

There are two ways for field and form customization:

  1. Global customization
pform library uses player::https://github.com/fafhrd91/pform/tree/master/examples library for customization. pform libriary defines layer category form for all templates.
  1. Field/form customization
Also it is possible to customize widget of input template for each field. You can pass tmpl_widget argument to to field constructor for widget customization and tmpl_input argument for input generation. Both arguments should be valid pyramid renderer path. Form accepts three different templates, tmpl_view, tmpl_actions and tmpl_widget. tmpl_view is form renderer, tmpl_actions if form buttons renderer, tmpl_widget is custom field widget renderer. If field does not use custom tmpl_widget then form automatically sets tmpl_widget for each of this fields.

Examples

There are several example. You can find them in the examples directory at github.

https://github.com/fafhrd91/pform/tree/master/examples

Requirements

  • Python 2.6+ or Python 3.2+
  • virtualenv

License

pform is offered under the BSD license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.