Posts tagged Web.py
October 01th, 2010
Web.py checkboxes
Topics: Web.py, Python, Howto, CodeCheckboxes are a bit tricky with web.py, here's how to get them working:
import web
from web import form
example_form = form.Form(
form.Checkbox("lovelycheckbox", description="lovelycheckbox", class_="standard", value="something.. Anything!"),
form.Button("Update checkbox", type="submit", description="Send"),
)
class grabresults:
def GET(self):
f = example_form()
return f.render()
def POST(self):
f = example_form()
theresult = f['lovelycheckbox'].checked
OK, so that's a dumb (and untested) example, but it gives you an idea. The 'theresult' variable is going to be true or false depending on if the 'thelovelycheckbox' was ticked or not.
Things to note:
- The checkbox has to be given a value, it's a bug with web.py.
- To get the result you use .checked instead of the normal .value
Cheers, Dave.
July 14th, 2009
web.py captcha form
Topics: Web.py, Python, CodeI coded a basic captcha for my web.py form, here’s how:
First I made a function that generates an image and returns it + the code (yea yea it’s a mess).
import Image, ImageDraw, ImageFont, cStringIO, random
def getCaptcha():
im = Image.new("RGB", (100, 60))
draw = ImageDraw.Draw(im)
for x in range(0, 100):
for y in range(0, 60):
draw.point((x, y), (135, 191, 107))
font = ImageFont.truetype('cracked.ttf', 50)
alphabet = 'abcdefghijklmnopqrstuvwxyz'
word = ''
for i in range(5):
word = word + alphabet[random.randint(0, len(alphabet) -1)]
draw.text((5, 5), word, font=font, fill=(0, 0, 0))
f = cStringIO.StringIO()
im.save(f, "GIF")
f.seek(0)
return word, f
Now here’s snippets of web.py code:
urls = (
...
'/captcha.gif', 'captcha'
)
if web.config.get('_session') is None:
session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'captcha': ''})
web.config._session = session
else:
session = web.config._session
vcaptcha = form.Validator('Please enter the code', lambda x:x == session.captcha)
enquiry_form = form.Form(
...
form.Textbox("captcha", vcaptcha, description="Validation Code", pre="<img src='/captcha.gif' valign=center><br>", class_="standard", style="width:70px;"),
)
class captcha:
def GET(self):
web.header("Content-Type", "image/gif")
captcha = getCaptcha()
session.captcha = captcha[0]
return captcha[1].read()
Also to make the form look prettier I modified web.py to give the table rows an id and then used css to alter their padding etc.
Sorry I didn’t take much time over this post, hopefully the code will get you started though, Dave.
July 10th, 2009
web.py tutorial + sqlite
Topics: Python, Web.py, HowtoIf you want to follow the web.py tutorial using sqlite here’s how:
Line for your python to connect to DB
db = web.database(dbn='sqlite', db='testdb')
To create the database type
sqlite3 testdb
and input this SQL:
CREATE TABLE todo (id integer primary key, title text, created date, done boolean default 'f');
CREATE TRIGGER insert_todo_created after insert on todo
begin
update todo set created = datetime('now')
where rowid = new.rowid;
end;
Now create an entry with this SQL
insert into todo (title) values ('Learn web.py');Finally quit sqlite3 when your ready by typing
.quit