如何从web2py RESTFUL服务创建自定义通用视图?

时间:2017-09-13 01:23:06

标签: python html rest model-view-controller web2py

使用Web2Py RESTFUL服务,我希望在导航到此链接(http://127.0.0.1:8000/app/default/api/example_data.html_table/?limit=1)后在浏览器中呈现以下html:

<div><table><thead><tr><th>example_data.id</th><th>example_data.Firstname</th><th>example_data.Lastname</th><th>example_data.Age</th></tr></thead><tbody><tr class="w2p_odd odd"><td>1</td><td>SUUUUPPPEEEERRRR LONGGGGG FIRSTTTT NAMEEEE</td><td>Smith</td><td>1</td></tr></tbody></table></div>

但是,我目前收到以下内容:

<div><table><thead><tr><th>example_data.id</th><th>example_data.Firstname</th><th>example_data.Lastname</th><th>example_data.Age</th></tr></thead><tbody><tr class="w2p_odd odd"><td>1</td><td>SUUUUPPPEEEER...</td><td>Smith</td><td>1</td></tr></tbody></table></div>

不同之处在于Web2Py正在缩短&#34; SUUUUPPPEEEERRRR LONGGGGG FIRSTTTT NAMEEEE&#34;到&#34; SUUUUPPPEEEER ......&#34;但我需要整个文本

我的视图名为generic.html_table,它生成以下内容:

{{=BEAUTIFY(response._vars[next(iter(response._vars))])}}

控制器

@request.restful()
def api():
    response.view = 'generic.' + request.extension
    def GET(*args,**vars):
        patterns = 'auto'
        parser = db.parse_as_rest(patterns,args,vars)
        return dict(content=parser.response)
    def POST(table_name,**vars):
        if 'id' in vars.keys():
            return db[table_name].update_or_insert(db[table_name]._id == vars['id'],**vars)
        else:
            return db[table_name].validate_and_insert(**vars)
    def PUT(table_name,**vars):
        record_id = vars['id']
        return db(db[table_name]._id==record_id).update(**vars)
    def DELETE(table_name,record_id):
        return db(db[table_name]._id==record_id).delete()
    return dict(GET=GET, POST=POST, PUT=PUT, DELETE=DELETE)

MODEL

db.define_table('example_data', Field('Firstname', 'string'),Field('Lastname', 'string'),Field('Age', 'integer'))

我还尝试了以下观点:

{{=response._vars[next(iter(response._vars))]}}

结果(名字仍然被切断):

<table><thead><tr><th>example_data.id</th><th>example_data.Firstname</th><th>example_data.Lastname</th><th>example_data.Age</th></tr></thead><tbody><tr class="w2p_odd odd"><td>1</td><td>Jill</td><td>Smith</td><td>1</td></tr><tr class="w2p_even even"><td>2</td><td>Eve</td><td>Jackson</td><td>33</td></tr><tr class="w2p_odd odd"><td>3</td><td>afdaskfdlasjf...</td><td>Jackson</td><td>33</td></tr><tr class="w2p_even even"><td>4</td><td>SUUUUPPPEEEER...</td><td>Jackson</td><td>33</td></tr></tbody></table>

我还尝试了以下观点:

{{=XML(response._vars[next(iter(response._vars))])}}

结果(丢失所有HTML格式):

example_data.id,example_data.Firstname,example_data.Lastname,example_data.Age     1,吉尔,史密斯,1     2,伊芙,杰克逊,33     3,afdaskfdlasjfkdlsjfklajdfskasjfklsdajfdklsajfklsajfdskalfdjsakldfjklasfjkdlsajfdsakljdklsadcjklasjcklsjackldsjakfldajsfklasdfjklasjfdklajfdsklsjafkldasjfkldasjkldsjcklajsckljackldajsdfklfjkalsncklacnkalsdfjkldasnckldasjckljsdaklfdnfkldsajfdklasjldsk,杰克逊,33     4,SUUUUPPPEEEERRRR LONGGGGG FIRSTTTT NAMEEEE,Jackson,33

1 个答案:

答案 0 :(得分:0)

所有这些方法最终都会导致DAL Rows对象传递给SQLTABLE以在视图中呈现。默认情况下,字段被截断为16个字符。如果您想更改它,则必须明确调用SQLTABLE

{{=SQLTABLE(content, truncate=100)}}