Reputation: 1545
model :
db.define_table('files',
Field('course_id', 'reference course'),
Field('documentx_filename',unique=True),
Field('documentx','upload'))
controller:
def show_doc():
rows = db( (db.course.id == db.files.course_id) & (db.dept.id==db.course.dept_id) ).select()
return dict(rows=rows)
def create_doc():
form = SQLFORM(db.files).process(next=URL('show_doc'))
if request.vars.documentx != None:
form.vars.documentx_filename = request.vars.documentx.filename
if form.process().accepted:
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
else :
response.flash = 'something still went wrong'
return dict(form = form)
view file for show_doc:
<div><a href={{=URL('default', 'download', args=row.files.documentx)}}> file = {{=row.files.documentx_filename}}</a></div>
<br />
{{pass}}
now if I put the file name in the field "documentx_filename" then the file name is shown.
But if I don't put the name in the field "documentx_filename" and leave it empty but upload a file. It should copy the name of the file uploaded as in controller create_doc I put the if statement but it doest do so?
Upvotes: 2
Views: 75
Reputation: 659
You can store the original filename in the model (http://www.web2py.com/book/default/chapter/07#Storing-the-original-filename)
But, assuming you have the same model as in this post : How can I join 3 tables and output all three together joined in web2py? I would add a "title" field in the model definition :
db.define_table('files',
Field('title', unique=True, requires=IS_NOT_EMPTY()),
Field('course_id', 'reference course'),
Field('documentx','upload'))
Then, in your view you can write :
{{for row in rows:}}
<div><a href={{=URL('default', 'download', args=row.files.documentx)}}>row.files.title</a></div>
{{pass}}
Upvotes: 2