Created
July 16, 2015 09:15
-
-
Save mdipierro/f95dab0f869f5504482e to your computer and use it in GitHub Desktop.
contact form in web2py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# append to scaffoling models/db.py | |
db.define_table('contact', | |
Field('your_name',requires=IS_NOT_EMPTY()), | |
Field('email',requires=IS_EMAIL()), | |
Field('message','text')) | |
# append to scaffolding controllers/default.py | |
def contact_us(): | |
form = SQLFORM(db.contact) | |
if form.process().accepted: | |
mail.send(to='[email protected]', | |
subject='contact request from %(your_name)s %(email)s' % form.vars, | |
message = form.vars.message) | |
session.flash = 'Thank you, your form was submitted' | |
redirect(URL('index')) | |
return dict(form=form) | |
# views/default/contact_us.html | |
{{extend 'layout.html'}} | |
{{=form}} | |
Configure the email in private/appconfig.ini. The database table will be create automatically. Runs on VPS and Google App Engine, with any SQL engine, MongoDB, and Google Datastore. Uses bootstrap 3 by default. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a suggested revision with following changes
db.define_table('contact',
Field('your_name',requires=IS_NOT_EMPTY()),
Field('email',requires=IS_EMAIL()),
Field('form_message','text', label='Message'))
append to scaffolding controllers/default.py
def contact_us():
form = SQLFORM(db.contact_form)
sent = False
if form.process().accepted:
sent = mail.send(to='[email protected]',
subject='auto-contact {name} {email}'.format(**form.vars),
reply_to=form.vars.email,
message=form.vars.form_message
)
if not sent:
session.flash = 'Error sending message, please use email.'
return dict(form=form, sent=sent)
views/default/contact_us.html
{{extend 'layout.html'}}
{{if sent:}}
Form submitted, thank you.
{{else:}} {{=form}} {{pass}}