I'm doing it it through AJAX so it's broken up into add_form and
add_item. Let me know if you need any clarification or additional
code.
add_form:
def add_form():
is_admin()
if not 't' in request.vars:
return
else:
for k in ('t', 'pid', 'ptype'):
globals()[k] = request.vars[k] if k in request.vars else
None
if pid: globals()['pid'] = int(pid)
fields = db[t].fields
if ptype and (ptype + '_id') in fields: fields.remove((ptype
+'_id'))
form_id = '%s-create-%i' % (t, random.randint(0,9999))
url_vars = dict(t=t, form_id=form_id)
if pid and ptype: url_vars.update(pid=pid, ptype=ptype)
url = adminc('add_item', vars=url_vars)
form = SQLFORM(db[t], fields = fields, _id=form_id, _class='admin-
form add-form', _action=url)
disambiguate_form_datetime_ids(form, t)
ret = dict(create_form = form, t=t, create_form_id=form_id, lbl =
ADMIN_TYPES_INDEX[t]['lbl'])
if ptype and pid: ret.update(ptype=ptype,pid=pid,
plbl=get_item_lbl(ptype,pid))
return ret
add_item:
def add_item():
is_admin()
if not 't' in request.vars:
return
else:
for k in ('t', 'pid', 'ptype','form_id'):
globals()[k] = request.vars[k] if k in request.vars else
None
if pid and pid != 'undefined': globals()['pid'] = int(pid)
#process join/m2m
fields = db[t].fields
if ptype:
if pid and (ptype + '_id') in fields:
request.vars[ptype + '_id'] = pid
m2m = False
else:
subtab = ADMIN_TYPES_INDEX[ptype]['subtab_types_index'][t]
m2m = 'm2m' in subtab and subtab['m2m']
m2m_table = get_m2m(t,ptype)
else: m2m = False
#create form
url_vars = dict(t=t, form_id=form_id)
if pid and ptype: url_vars.update(pid=pid, ptype=ptype)
url = adminc('add_item', vars=url_vars)
form = SQLFORM(db[t], _class = 'admin-form add-form', _id=form_id,
_action=url)
cleanvars = db[t]._filter_fields(request.vars)#dict( (k,
request.vars[k]) for k in request.vars if k in fields)
#check for preinsert hook
hooks = CRUD_HOOKS[t] if t in CRUD_HOOKS else None
if hooks and 'preinsert' in hooks:
res = hooks['preinsert'](t,cleanvars,ptype,pid)
if res and isinstance(res,str) and 'Error' in res: return res
elif res: cleanvars = res
#submit data
if form.accepts(cleanvars, formname=None, dbio = not m2m):
cache_update = [t]
if m2m:
id = db[t].insert(**cleanvars)
join_table = m2m_table or (ptype + 's_' + t + 's')
join_ins = { (t + '_id'):id, (ptype + '_id'):pid }
join_ins.update(dict((k, request.vars[k]) for k in
request.vars if k not in cleanvars and k in db[join_table].fields))
db[join_table].insert(**join_ins)
cache_update.append(join_table)
update_grid_cache(*cache_update)
print 'inserted'
return "success"
elif form.errors:
return form
On Jul 3, 4:23 pm, Massimo Di Pierro <[email protected]>
wrote:
> You managed to bypass the web2py upload mechanism and stored a
> FieldStorage object in the database. This is a mistake because it is
> difficult to take it out. The problem is probably not in your model
> but in the form you use to populate. Can you show us the action?
>
> On Jul 3, 2:29 pm, Lennon <[email protected]> wrote:
>
>
>
>
>
>
>
> > UPDATE:
>
> > The error I was getting was caused by a missing ')' in the view so
> > fixing that has gotten me to a new error.
>
> > I simplified my model to how you have pbreit just to make things
> > easier.
>
> > But when I try to display the image like so:
>
> > <img src="{{=URL('download', args=img['upload'])}}" />
>
> > I get a 400 error in the Chrome Console:
>
> >http://127.0.0.1:8000/sos_test/default/download/FieldStorage%28%27upl...
> > 400 (BAD REQUEST)
>
> > There is that field storage again. I tried doing
> > img['upload'].filename as recommended in this link:
>
> >http://groups.google.com/group/web2py/browse_thread/thread/9562da3b8b...
>
> > But I got an error that the object was a string and .fieldname
> > wouldn't work.
>
> > Thoughts?
>
> > I'm using SQLForm to upload the image. Maybe I'm doing something
> > strange upon upload to store it in such a weird format?
>
> > On Jul 3, 1:04 pm, pbreit <[email protected]> wrote:
>
> > > I would suggest following the Book more
> > > closely:http://web2py.com/book/default/chapter/03
>
> > > db.define_table('image',
> > > Field('caption'),
> > > Field('file', 'upload'))
>
> > > <img src="{{=URL('download', args=image.file)}}" />