输出上传文件表中存储的文件名?

2024-04-26 15:03:46 发布

您现在位置:Python中文网/ 问答频道 /正文

型号:

db.define_table('files',
                Field('course_id', 'reference course'),
                Field('documentx_filename',unique=True),
                Field('documentx','upload'))

控制器:

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)

查看显示文档的文件:

<div><a href={{=URL('default', 'download', args=row.files.documentx)}}> file = {{=row.files.documentx_filename}}</a></div>
<br />
{{pass}}

现在,如果我把文件名放在“documentx\u filename”字段中,那么会显示文件名。你知道吗

但是,如果我不把名字放在“documentx\u filename”字段中,让它为空,而是上传一个文件。 它应该复制在controller create\u doc中上传的文件名,我放了if语句,但它没有这样做?你知道吗


Tags: formidfielddbdocif文件名response
1条回答
网友
1楼 · 发布于 2024-04-26 15:03:46

可以将原始文件名存储在模型(http://www.web2py.com/book/default/chapter/07#Storing-the-original-filename)中

但是,假设您拥有与本文相同的模型:How can I join 3 tables and output all three together joined in web2py? 我将在模型定义中添加一个“title”字段:

db.define_table('files',
                Field('title', unique=True, requires=IS_NOT_EMPTY()),
                Field('course_id', 'reference course'),
                Field('documentx','upload'))

然后,在你看来,你可以写:

{{for row in rows:}}
    <div><a href={{=URL('default', 'download', args=row.files.documentx)}}>row.files.title</a></div>
{{pass}}

相关问题 更多 >