Here is my code:
Controller
def index():
form = SQLFORM(db.guest_questionnaire)
if form.process().accepted:
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = ''
return dict(form=form)
Model
db.define_table('contact_time',
Field('contact_time', 'string'),
format='%(contact_time)s'
)
db.define_table('guest_questionnaire',
Field('best_times_to_reach', 'list:reference
contact_time',widget=SQLFORM.widgets.checkboxes.widget)
)
View
<div id = "best_times_to_reach" class="col-md-3 col-lg-3 col-sm-12
col-xs-12">
<label for="best_times_to_reach">Best Times to Reach You</label>
{{=form.custom.widget.best_times_to_reach}}
</div>
The form renders just fine in the view, but when it is submitted, I get a
Foreign Key Constraint Error. Any ideas as to why?
<class 'sqlite3.IntegrityError'> FOREIGN KEY constraint
failedVersionweb2py™Version
2.11.2-stable+timestamp.2015.05.30.16.33.24PythonPython 2.7.3:
/Users/travismay/Downloads/web2py/web2py.app/Contents/MacOS/python (prefix:
/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources)Traceback
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
Traceback (most recent call last):
File
"/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/restricted.py",
line 227, in restricted
File
"/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/applications/betsy/controllers/default.py"
<http://127.0.0.1:8000/admin/default/edit/betsy/controllers/default.py>, line
70, in <module>
File
"/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/globals.py",
line 412, in <lambda>
File
"/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/applications/betsy/controllers/default.py"
<http://127.0.0.1:8000/admin/default/edit/betsy/controllers/default.py>, line
20, in index
File
"/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/html.py",
line 2301, in process
File
"/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/html.py",
line 2238, in validate
File
"/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/sqlhtml.py",
line 1688, in accepts
File
"/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/packages/dal/pydal/objects.py",
line 691, in insert
File
"/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/packages/dal/pydal/adapters/base.py",
line 731, in insert
IntegrityError: FOREIGN KEY constraint failed
Error snapshot [image: help]
<http://127.0.0.1:8000/admin/default/ticket/betsy/127.0.0.1.2015-06-14.07-57-30.642aa5d4-885e-4201-9f76-c79c7a932773#>
<class 'sqlite3.IntegrityError'>(FOREIGN KEY constraint failed)
inspect attributes
Frames
-
*File
/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/restricted.py
in restricted at line 227* code arguments variables
-
*File
/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/applications/betsy/controllers/default.py
in <module> at line 70* code arguments variables
-
*File
/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/globals.py
in <lambda> at line 412* code arguments variables
-
*File
/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/applications/betsy/controllers/default.py
in index at line 20* code arguments variables
-
*File
/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/html.py
in process at line 2301* code arguments variables
-
*File
/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/html.py
in validate at line 2238* code arguments variables
-
*File
/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/sqlhtml.py
in accepts at line 1688* code arguments variables
-
*File
/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/packages/dal/pydal/objects.py
in insert at line 691* code arguments variables
-
*File
/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/gluon/packages/dal/pydal/adapters/base.py
in insert at line 731* code arguments variables
Function argument list
(self=<pydal.adapters.sqlite.SQLiteAdapter object>, table=<Table
guest_questionnaire
(id,first_name,last_n...onne,purchaser_type,monthly_newsletter,comments)>,
fields=[(<pydal.objects.Field object>, 1), (<pydal.objects.Field object>,
''), (<pydal.objects.Field object>, 20), (<pydal.objects.Field object>, 8),
(<pydal.objects.Field object>, 5), (<pydal.objects.Field object>, ''),
(<pydal.objects.Field object>, 1), (<pydal.objects.Field object>, 3),
(<pydal.objects.Field object>, ''), (<pydal.objects.Field object>, ''),
(<pydal.objects.Field object>, 2), (<pydal.objects.Field object>, 11),
(<pydal.objects.Field object>, ['1']), (<pydal.objects.Field object>, ''),
(<pydal.objects.Field object>, 1), (<pydal.objects.Field object>, ''),
(<pydal.objects.Field object>, 2), (<pydal.objects.Field object>, ''),
(<pydal.objects.Field object>, ''), (<pydal.objects.Field object>, ''),
...])
Code listing
726.
727.
728.
729.
730.
731.
732.
733.
734.
735.
self.execute(query)
except Exception:
e = sys.exc_info()[1]
if hasattr(table,'_on_insert_error'):
return table._on_insert_error(table,fields,e)
raise e
if hasattr(table, '_primarykey'):
mydict = dict([(k[0].name, k[1]) for k in fields if k[0].name in
table._primarykey])
if mydict != {}:
return mydict
Variables
Context
locals request session response
In file:
/Users/travismay/Downloads/web2py/web2py.app/Contents/Resources/applications/betsy/controllers/default.py
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without limitations
#########################################################################
## This is a sample controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does streaming)
#########################################################################
from gluon.tools import Mail
mail = Mail()
mail.settings.server = 'smtp.gmail.com:587'
mail.settings.sender = 'Private'
mail.settings.login = 'Private:Private'
def index():
form = SQLFORM
<http://127.0.0.1:8000/examples/global/vars/SQLFORM>(db.guest_questionnaire)
if form.process().accepted:
response <http://127.0.0.1:8000/examples/global/vars/response>.flash =
'form accepted'
mail.send(to=['Private'],
subject='New Record',
message="Name: " + form.vars.first_name + " " + form.vars.last_name
)
elif form.errors:
response <http://127.0.0.1:8000/examples/global/vars/response>.flash =
'form has errors'
else:
response <http://127.0.0.1:8000/examples/global/vars/response>.flash =
''
return dict(form=form)
def user():
"""
exposes:
http://..../[app]/default/user/login
http://..../[app]/default/user/logout
http://..../[app]/default/user/register
http://..../[app]/default/user/profile
http://..../[app]/default/user/retrieve_password
http://..../[app]/default/user/change_password
http://..../[app]/default/user/manage_users (requires membership in
use @auth.requires_login()
@auth.requires_membership('group name')
@auth.requires_permission('read','table name',record_id)
to decorate functions that need access control
"""
return dict(form=auth())
@cache <http://127.0.0.1:8000/examples/global/vars/cache>.action()
def download():
"""
allows downloading of uploaded files
http://..../[app]/default/download/[filename]
"""
return response
<http://127.0.0.1:8000/examples/global/vars/response>.download(request
<http://127.0.0.1:8000/examples/global/vars/request>, db)
def call():
"""
exposes services. for example:
http://..../[app]/default/call/jsonrpc
decorate with @services.jsonrpc the functions to expose
supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv
"""
return service()
response <http://127.0.0.1:8000/examples/global/vars/response>._vars=response
<http://127.0.0.1:8000/examples/global/vars/response>._caller(index)
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.