tags:

views:

343

answers:

2

i used rows.xml() to generate html output. i want to know how to add html codes to this generated html page e.g: "add logo, link css file,.. etc"

rows=db(db.member.membership_id==request.args[0]).select(db.member.membership_id ,db.member.first_name,db.member.middle_name ,db.member.last_name) return rows.xml()

A: 

Just prepend/append it to the string that rows.xml() returns:

html = '<html><head>...</head><body>' + rows.xml() + '</body></html>'
Aaron Digulla
i wrote this line: data = "<html><body>"+rows.xml+"</body></html>" and,it displayed the following error: 'TypeError: cannot concatenate 'str' and 'instancemethod' objects'
Neveen
Oops... I forgot the "()" after xml. Fixed.
Aaron Digulla
+1  A: 

There are many HTML helpers you can use, for example:

html_code = A('<click>', rows.xml(), _href='http://mylink')
html_code = B('Results:', rows.xml(), _class='results', _id=1)
html_page = HTML(BODY(B('Results:', rows.xml(), _class='results', _id=1)))

and so on.

You can even create a whole table automatically:

table = SQLTABLE(rows, orderby=True, _width="100%")

and then pick it apart to insert links or modify its elements.

It is very powerful and normally you don't have to bother writing the actual HTML yourself. Here is the cheatsheet, or you can check directly on the website documentation.


Edit: Just to make sure, you don't actually need to generate the whole HTML page, it is easier to let web2py insert your response in a template that has the same name as your controller (or force a particular template with response.view = 'template.html'. The documentation tutorial will explain that better and in further details.

In a few words, if you are implementing the function index, you could either return a string (the whole page HTML, which seems to be what you are heading for), or a dictionary to use templates.

In the first case, just code your function like this:

def index():
    # ... code to extract the rows
    return HTML(BODY(B('Results:', rows.xml(), _class='results', _id=1))).xml()

Otherwise, write an html template in views/controller/index.html (or another file if you insert the response.view=... in your function, to re-use the same template), which could be like this:

<html><head></head>
  <body>
    {{=message}}
  </body>
</html>

and return a dictionary:

def index():
    # ... code to extract the rows
    html = B('Results:', rows.xml(), _class='results', _id=1)
    return dict(message=html)
RedGlyph
Thanks for your help.after adding the previous lines to my function what to write at the return of function "instead of return rows.xml() to display a html page"
Neveen
You're welcome. I've just added a bit for the whole page (which was the original question), check the Edit: remark though.
RedGlyph
i am not understand how >response.view = 'template.html' can ghelp me to fix my problem. i checked the document and i dont understand.Thanks
Neveen
i get it, Thanks so much
Neveen
i tried the first line "return HTML(BODY(B('Results:', rows.xml(), _class='results', _id=1)))" and it displayed html tages into generated html page.The second one displayed an error "NameError: global name 'ODY' is not defined" i removed it and the same html tages displayed. i dont know why?
Neveen
I've never tried to return the whole HTML code, but I suppose it's necessary to extract the xml first, so maybe something along `return HTML(...).xml()` is the correct way to do that.
RedGlyph
The 'ODY' was a typo (I'm especially good at them today) :-) Hopefully fixed the post.
RedGlyph