Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render tuples in bottlepy

I have been using bottlepy and i have a thing like this:

..code..
comments = [(u'34782439', 78438845, 6, u'hello im nick'), 
(u'34754554', 7843545, 5, u'hello im john'), 
(u'332432434', 785345545, 3, u'hello im phil')] 

return comments

In the view i have done this:

%for address date user text in comments:
      <h3>{{address}}</h3>
      <h3>{{date}}</h3>
      <h3>{{user}}</h3>
      <h3>{{text}}</h3>
%end

When i start the server, the error is:

Error 500: Internal Server Error

Sorry, the requested URL http://localhost:8080/hello caused an error:

Unsupported response type: <type 'tuple'>

How could i render it into the view?

(im sorry for my english)

like image 451
Nicky Avatar asked Feb 23 '26 23:02

Nicky


1 Answers

Your code has two problems. First, response cannot be a list of tuples. It can be a string or a list of strings, as Peter suggests, or, in case you want to use the view, it can (and should) be a dictionary of view variables. Keys are variable names (these names, such as comments, will be available in the view), values are arbitrary objects.

So, your handler function can be rewritten as:

@route('/')
@view('index')
def index():
    # code
    comments = [
        (u'34782439', 78438845, 6, u'hello im nick'), 
        (u'34754554', 7843545, 5, u'hello im john'), 
        (u'332432434', 785345545, 3, u'hello im phil')]
    return { "comments": comments }

Notice the @view and @route decorators.

Now, you have a problem in your view code: the commas in tuple unpacking are missing. Therefore, your view (named index.html in my case) should look like:

%for address, date, user, text in comments:
    <h3>{{address}}</h3>
    <h3>{{date}}</h3>
    <h3>{{user}}</h3>
    <h3>{{text}}</h3>
%end
like image 198
Helgi Avatar answered Feb 25 '26 11:02

Helgi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!