SQLFORM.factory upload field can't show link

115 views
Skip to first unread message

killzane

unread,
Feb 25, 2016, 4:19:10 AM2/25/16
to web2py-users
This is my code
form = SQLFORM.factory(Field('list_name', 'string', label='List Name'), db.project, table_name='project',upload=URL('download'))

And here is my db.py about upload field
Field('project_pdf', 'upload', label='Project PDF', comment='', uploadfolder='app/uploads'),

When I use web2py's appadmin I can see the link like this picture




but in my view I use 
{{=form}}

There are only choose file but not file link

how could I do for it?

Anthony

unread,
Feb 25, 2016, 9:41:32 AM2/25/16
to web2py-users
There should only be a file link if you are editing an existing record (with an existing file). If you are displaying a "create" form, to what file would you expect such a link to point?

Anthony

killzane

unread,
Feb 25, 2016, 10:50:12 AM2/25/16
to web2py-users
Yes, I mean when I want to edit an existing record, there are no link. 
But I can find the file when use appadmin.

Anthony於 2016年2月25日星期四 UTC+8下午10時41分32秒寫道:

Anthony

unread,
Feb 25, 2016, 11:19:03 AM2/25/16
to web2py-users
Can you show the code you are using to edit an existing record via SQLFORM.factory?

killzane

unread,
Feb 26, 2016, 12:29:45 AM2/26/16
to web2py-users
This is the code in my controller
form = SQLFORM.factory(Field('list_name', 'string', label='List Name'), db.project, table_name='project',upload=URL('download'))

for t in db.project:
    query = (t.id == request.vars.id
    __copydata(db(query).select(limitby=(0,1)).first(), form.vars, t.fields)

and this is in view
<div>{{=form}}</div>



Anthony於 2016年2月26日星期五 UTC+8上午12時19分03秒寫道:

Anthony

unread,
Feb 26, 2016, 8:08:13 AM2/26/16
to web2py-users

for t in db.project:
    query = (t.id == request.vars.id
    __copydata(db(query).select(limitby=(0,1)).first(), form.vars, t.fields)

The above is confusing and cannot be the actual code, as it would raise an exception. When you iterate over db.project, you get its Field objects (so each value of "t" is a Field object). Field objects do not have ".id" or ".fields" attributes, so both of the next two lines would result in errors. Perhaps you instead mean to be iterating over Table objects, but it's not clear why you would be doing that, as the form is based on just a single table.

Anthony

killzane

unread,
Feb 28, 2016, 3:48:26 AM2/28/16
to web2py-users
Because I want to add other field to the form so I use SQLFORM.factory.

And this is my __copydata method
def __copydata(src, dest, fields):
    if src:
        for k in fields:
            if src.has_key(k):
                dest[k] = src[k]
    return dict()



Anthony於 2016年2月26日星期五 UTC+8下午9時08分13秒寫道:

Anthony

unread,
Feb 28, 2016, 8:29:52 AM2/28/16
to web2py-users
Sorry, still not clear what you are trying to do, and you have not explained why you are iterating over the Fields of the db.project table.

killzane

unread,
Feb 28, 2016, 10:07:28 AM2/28/16
to web...@googlegroups.com
I want to modify a record from db.project, and there are a reference field in db.project.
So I use SQLFORM.factory to add field to put reference list name.

the code I paste is simplify version
here is full code
project = db((db.project.id == 1) & (db.frature.project_id == 1)).select(db.project.ALL, db.feature.ALL).first()

form = SQLFORM.factory(Field('list_name', 'string', label='List Name'), db.project, db.feature, table_name='project',upload=URL('download'))

for t in [db.project, db.project_feature]:
  query = t.id == project.id if t == db.project else t.project_id == project.id
  __copydata(db(query).select(limitby=(0,1)).first(), form.vars, t.fields)

so, that me ask another question:
How to make SQLFORM.widgets.upload.widget work.
I mean make the link button appear.
what content I need to put in form.var?

Anthony於 2016年2月28日星期日 UTC+8下午9時29分52秒寫道:

Anthony

unread,
Feb 28, 2016, 7:51:27 PM2/28/16
to web2py-users
The widget gets created when the form is initialized, so if you want to specify a value for the file URL, you should do it by setting the "default" attribute of the upload field in the db.project table before creating the table.

Anthony

killzane

unread,
Feb 29, 2016, 2:32:55 AM2/29/16
to web2py-users
So if I have this field in db.project
Field('project_pdf', 'upload', label='Project PDF', comment='', uploadfolder='app/uploads'),

I must change to 
Field('project_pdf', 'upload', label='Project PDF', comment='', uploadfolder='app/uploads', default=''),
what content should I put in "default" attribute?
the file name? or folder path?
records have same folder path but different file name.

Even if I change "form.var.project_pdf", the link button won't appear?


Anthony於 2016年2月29日星期一 UTC+8上午8時51分27秒寫道:

Anthony

unread,
Feb 29, 2016, 1:24:48 PM2/29/16
to web2py-users
The default value should be the file name, but you don't have to set it when you define the table in the model. Instead, set the default in the controller right before you create the form:

db.project.project_pdf.default = the_file_name

Simply setting form.vars.project_pdf will not work.

Anthony

killzane

unread,
Mar 1, 2016, 6:59:59 AM3/1/16
to web2py-users
It's work!
Thanks!

Anthony於 2016年3月1日星期二 UTC+8上午2時24分48秒寫道:
Reply all
Reply to author
Forward
0 new messages