0

度过了漫长的一天,希望能得到一些帮助:)

我正在 web2py 中开发一个搜索表单,我创建这样的表单

<table>    
<form method="post" action="{{=URL('views', 'search')}}">
    <tr><td><label>Enter Keywords</label></td><td><input style="background-color: lightgreen; color:#333; font-weight:bold; width:260px;" type="text" name="tags"></td>
        </tr>
        <tr>
        <td>
        <label>Select Region</label>
            </td>
            <td>
                <select name="region" style="width:260px">
    <option value="" style="background-color: darkgreen; color:#FEFEFE; font-weight:bold; width:260px;">Choose a Region...

    {{for region in region_query:}}
    <option value="{{=region.id}}" style="background-color: lightgreen; color:#333; font-weight:bold; width:260px;">{{=region.title}}</option>
    {{pass}}
    </select>
    </td>
    </tr>
    <tr>
            <td>
        <label>Select Category</label>
            </td>
    <td>

    <select name="category" style="width:260px;">

    <option value="" style="background-color: darkgreen; color:#FEFEFE; font-weight:bold; width:260px;">Choose a Category
    {{for cat in cat_query:}}
    <optgroup label="{{=cat.title}}" style="background-color: darkgreen; color:#FEFEFE; font-weight:bold; width:260px;">
    {{sub_query = db(db.sub_category.main_category == cat.id).select(orderby=db.sub_category.title)}}
    </optgroup>
    {{for sub in sub_query:}}
    <option value="{{=sub.id}}" style="background-color: lightgreen; color:#333; width:260px;">{{=sub.title}}</option>

    {{pass}}
    {{pass}}
    </select>



    </td>
    </tr>
    <tr>
<td colspan="2" align="right">
    <input type="submit" style="background-color: lightgreen; color:#333; font-weight:bold; width:260px; height:30px" value="Search Business Directory">
    </td>
    </tr>
</form>
</table>

我的控制器由这个组成

def search():
    mystr = str(request.post_vars["tags"])
    if mystr == "" or mystr == None:
        return redirect(URL('default', 'error_search'))
    region = str(request.post_vars["region"])
    if region == "" or region == None:
        return redirect(URL('default', 'error_search'))
    category = str(request.post_vars["category"])
    if category == "" or mystr == None:
        return redirect(URL('default', 'error_search'))
    featured = db((db.featured_listing.tag_words.like('%'+mystr+'%')) | (db.listing.region.contains(region)) | (db.listing.sub_category.contains(category))).select(orderby=db.featured_listing.title)
    query = (db.listing.tag_words.like('%'+mystr+'%')) | (db.listing.region.contains(region)) | (db.listing.sub_category.contains(category))
    orderby = db.listing.title
    pcache = (cache.ram, 15)
    paginate = Pagination(db, query, orderby, display_count=10, cache=pcache, r=request, res=response)
    search_query = paginate.get_set(set_links=True)
    return dict(search_query=search_query, featured=featured)

我对结果搜索的看法包含这个

{{for query2 in featured:}}                                                                        

    <div class="listingItem">
    <div class="listingItem-content">
        <h2>{{=query2.title}}</h2>
    ....

等等等等

搜索时收到此错误

Traceback (most recent call last):
  File "/var/www/web2py/gluon/restricted.py", line 212, in restricted
    exec ccode in environment
  File "/var/www/web2py/applications/GreenPages_web/views/views/search.html", line 47, in <module>
AttributeError: 'Row' object has no attribute 'title'

知道如何解决这个问题并达到我想要的结果。

感谢您提供的任何建议。

4

1 回答 1

0
featured = db((db.featured_listing.tag_words.like('%'+mystr+'%')) |
    (db.listing.region.contains(region)) |
    (db.listing.sub_category.contains(category))).select(orderby=db.featured_listing.title)

上面的查询引用了两个不同的表(db.featured_listingdb.listing)。因此,结果将包括两个表中的字段,因此您需要同时使用表名和字段名来引用每条记录中的字段。如果“标题”字段在db.featured_listing表中,则必须通过query2.featured_listing.title而不是query2.title.

请注意,尽管您的查询引用了两个表,但它不涉及联接,因此此查询可能不会返回您期望的结果。

请参阅http://web2py.com/books/default/chapter/29/06#Inner-joins

于 2013-01-27T17:15:25.670 回答