Differences between revisions 4 and 5
Revision 4 as of 2012-04-18 02:50:17
Size: 2786
Comment:
Revision 5 as of 2012-04-21 03:57:51
Size: 4795
Comment: adding tw2 and validator
Deletions are marked like this. Additions are marked like this.
Line 89: Line 89:
=== widget ===
 *Adding toscawidget to pyramid:
 *http://toscawidgets.org/documentation/tw2.core/pyramid.html
 *http://pythonpaste.org/deploy/#filter-composition
 *First add these to setup.py:
{{{
requires=[

    ...

    "tw2.core",
    "tw2.forms",
    "tw2.dynforms",
    "tw2.sqla",
    "tw2.jqplugins.jqgrid",
    ],
}}}
 *Then run:
{{{
python setup.py develop
}}}
 *Now edit development.ini and add this on first line:
{{{
[pipeline:main]
pipeline =
    tw2.core
    myapp

[filter:tw2.core]
use = egg:tw2.core#middleware
}}}
 *And right below change app:main
from
{{{
[app:main]
use = egg:...
}}}
to
{{{
[app:myapp]
use = egg:...
}}}

 *Now we are ready to add the widget. Go to the view.py and add above your view
{{{
from tw2.forms import TableForm, TextField, CalendarDatePicker, SingleSelectField, TextArea
import tw2
#Validator
#from formencode.validators import Int, NotEmpty, DateConverter, DateValidator,PostalCode,String,Email
Line 91: Line 140:
class AddressForm(TableForm):
    # This WidgetsList is just a container
    #class fields(WidgetsList):
    FirstName = TextField(validator=tw2.core.Required)
    LastName = TextField(validator=tw2.core.Required)
    MaidenLastName = TextField()
    Email = TextField(validator=tw2.core.EmailValidator)
    Address = TextField(validator=tw2.core.Required)
    City = TextField(validator=tw2.core.Required)
    State = TextField(validator=tw2.core.Required)
    #Or you could do:
    #StateChoices = (("IL"),
    # ("IN"),
    # ("MS"),
    # )
    #State = SingleSelectField(options=StateChoices, validator=NotEmpty)
    ZipCode = TextField(size=5, validator=tw2.core.IntValidator)
    DOB = CalendarDatePicker(validator=tw2.core.DateValidator)
    GenderChoices = (("Female"),
                     ("Male"),
                    )
    Gender = SingleSelectField(options=GenderChoices)
    Description = TextArea(attrs=dict(rows=3, cols=25))
}}}

Addressbook

structure

You probably created your alchemy scaffold, and here is your folder structure:

myapp/
├── CHANGES.txt
├── development.ini
├── MANIFEST.in
├── myapp
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── scripts
│   │   ├── initializedb.py
│   │   ├── initializedb.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── static
│   │   ├── favicon.ico
│   │   ├── footerbg.png
│   │   ├── headerbg.png
│   │   ├── ie6.css
│   │   ├── middlebg.png
│   │   ├── pylons.css
│   │   ├── pyramid.png
│   │   ├── pyramid-small.png
│   │   └── transparent.gif
│   ├── templates
│   │   └── mytemplate.pt
│   ├── tests.py
│   ├── tests.pyc
│   ├── views.py
│   └── views.pyc
├── myapp.db
├── myapp.egg-info
│   ├── dependency_links.txt
│   ├── entry_points.txt
│   ├── not-zip-safe
│   ├── PKG-INFO
│   ├── requires.txt
│   ├── SOURCES.txt
│   └── top_level.txt
├── production.ini
├── README.txt
├── setup.cfg
└── setup.py

views.py

  • This will be the primary file where we will be working on our web application
  • Lets add our add function that will allow you to enter new addressbook.
  • Edit init.py and add the new add_route.

config.add_route('add', '/add')
  • This will add a route "add" and the url that it will use is "/add"
  • Now lets create a view that will handle our form.
  • Edit views.py and add our new "add view handler.

@view_config(route_name='add', renderer='templates/add.pt')
def add_view(request):
    '''This is an add view. Here is where we define and pass any information to the template.'''
    #Code here
    return {'project':'myapp'}

add template

  • Lets go to templates folder and create our add.pt
  • To simplify our coding we will copy an existing template and name it add.pt

cd templates
cp mytemplate.pt add.pt
  • Then replace the text:

#From:

Welcome to <span class="app-name">${project}</span>, an application generated by<br/>the Pyramid web application development framework.

#To:

Welcome to <span class="app-name">${project}</span>.<br/> Please add your address.
  • Lets check if it works, and our next step will be to add the form.

add1.png

widget

requires=[

    ...

    "tw2.core",
    "tw2.forms",
    "tw2.dynforms",
    "tw2.sqla",
    "tw2.jqplugins.jqgrid",
    ],
  • Then run:

python setup.py develop
  • Now edit development.ini and add this on first line:

[pipeline:main]
pipeline =
    tw2.core
    myapp

[filter:tw2.core]
use = egg:tw2.core#middleware
  • And right below change app:main

from

[app:main]
use = egg:...

to

[app:myapp]
use = egg:...
  • Now we are ready to add the widget. Go to the view.py and add above your view

from tw2.forms import TableForm, TextField, CalendarDatePicker, SingleSelectField, TextArea
import tw2
#Validator
#from formencode.validators import Int, NotEmpty, DateConverter, DateValidator,PostalCode,String,Email


class AddressForm(TableForm):
    # This WidgetsList is just a container
    #class fields(WidgetsList):
    FirstName = TextField(validator=tw2.core.Required)
    LastName = TextField(validator=tw2.core.Required)
    MaidenLastName = TextField()
    Email = TextField(validator=tw2.core.EmailValidator)
    Address = TextField(validator=tw2.core.Required)
    City = TextField(validator=tw2.core.Required)
    State = TextField(validator=tw2.core.Required)
    #Or you could do:
    #StateChoices = (("IL"),
    #                 ("IN"),
    #                 ("MS"),
    #                )
    #State = SingleSelectField(options=StateChoices, validator=NotEmpty)
    ZipCode = TextField(size=5, validator=tw2.core.IntValidator)
    DOB = CalendarDatePicker(validator=tw2.core.DateValidator)
    GenderChoices = (("Female"),
                     ("Male"),
                    )
    Gender = SingleSelectField(options=GenderChoices)
    Description = TextArea(attrs=dict(rows=3, cols=25))

create database model

MyWiki: Pyramid/addressbook (last edited 2012-05-23 03:02:26 by LukaszSzybalski)